0% found this document useful (0 votes)
9 views6 pages

Class 7

Lol..

Uploaded by

shdwshew
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views6 pages

Class 7

Lol..

Uploaded by

shdwshew
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Hanish statistics and training institute 8185815527

R Math
Simple Math
In R, you can use operators to perform common mathematical operations on
numbers.

The + operator is used to add together two values:

Example
10 + 5

Try it Yourself »

And the - operator is used for subtraction:

Example
10 - 5

Try it Yourself »
You will learn more about available operators in our R Operators Tutorial.

Built-in Math Functions


R also has many built-in math functions that allows you to perform
mathematical tasks on numbers.

For example, the min() and max() functions can be used to find the lowest or
highest number in a set:

Example

Hanish statistics and training institute 8185815527


Hanish statistics and training institute 8185815527

max(5, 10, 15)

min(5, 10, 15)

Try it Yourself »

sqrt()
The sqrt() function returns the square root of a number:

Example
sqrt(16)

Try it Yourself »

abs()
The abs() function returns the absolute (positive) value of a number:

Example
abs(-4.7)

Try it Yourself »

ceiling() and floor()


The ceiling() function rounds a number upwards to its nearest integer, and
the floor() function rounds a number downwards to its nearest integer, and
returns the result:

Example
Hanish statistics and training institute 8185815527
Hanish statistics and training institute 8185815527

ceiling(1.4)

floor(1.4)

Try it Yourself »

R Strings
String Literals
Strings are used for storing text.

A string is surrounded by either single quotation marks, or double quotation


marks:

"hello" is the same as 'hello':

Example
"hello"
'hello'

Try it Yourself »

Assign a String to a Variable


Assigning a string to a variable is done with the variable followed by the <-
operator and the string:

Example
str <- "Hello"
str # print the value of str

Hanish statistics and training institute 8185815527


Hanish statistics and training institute 8185815527

Try it Yourself »

Multiline Strings
You can assign a multiline string to a variable like this:

Example
str <- "Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua."

str # print the value of str

Try it Yourself »

However, note that R will add a "\n" at the end of each line break. This is called
an escape character, and the n character indicates a new line.

If you want the line breaks to be inserted at the same position as in the code,
use the cat() function:

Example
str <- "Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua."

cat(str)

Try it Yourself »

Hanish statistics and training institute 8185815527


Hanish statistics and training institute 8185815527

Escape Characters
To insert characters that are illegal in a string, you must use an escape
character.

An escape character is a backslash \ followed by the character you want to


insert.

An example of an illegal character is a double quote inside a string that is


surrounded by double quotes:

Example
str <- "We are the so-called "Vikings", from the north."

str

Result:

Error: unexpected symbol in "str <- "We are the so-called "Vikings"

Try it Yourself »

To fix this problem, use the escape character \":

Example
The escape character allows you to use double quotes when you normally would
not be allowed:

str <- "We are the so-called \"Vikings\", from the north."

str
cat(str)

Try it Yourself »
Hanish statistics and training institute 8185815527
Hanish statistics and training institute 8185815527

Hanish statistics and training institute 8185815527

You might also like