2+4
[1] 6
(AST230) R for Data Science
2+4
[1] 6
6
.[1]
you see in the returned value. That’s just R saying that 6
is the 1st value (and only value here) of the output line.(59 + 73 + 2) / 3
[1] 44.66667
10^(3+1)
[1] 10000
Description | Operator |
---|---|
Addition | + |
Subtraction | - |
Multiplication | * |
Division | / |
Exponentiation | ^ or ** |
integer division 10%/%3 is 3 |
x %/% y |
modulus (x mod y) 10%%3 is 1 |
x %% y |
Use R to calculate the following:
The above calculations do not produce any kind of output that is remembered by R
To store our calculations in R we need to give it a name and tell R to store that as an object.
Assignment operators <-
or =
can be used to assign a value to an R object, and it is preferable to use <-
as =
has other uses.
my_obj <- 48
Now that we’ve created this object, R knows all about it and will keep track of it during this current R session
All of the objects you create will be stored in the current workspace and you can view all the objects in your workspace in RStudio by clicking on the Environment
tab in the top right-top pane
my_obj2 <- "R is cool"
my_obj2
and assigned it a value of R is cool
which is a character string.<- R is cool my_obj2
Error in parse(text = input): <text>:1:14: unexpected symbol
1: my_obj2 <- R is
^
my_obj2
listed as type character.To change the value of an existing object we simply reassign a new value to it.
For example, to change the value of my_obj2
from "R is cool"
to the number 1024
my_obj2 <- 1024
Once we have created a few objects, we can do stuff with our objects.
For example, the following code creates a new object my_obj3
and assigns it the value of my_obj
added to my_obj2
which is 1072 (48 + 1024 = 1072)
my_obj3 <- my_obj + my_obj2
my_obj3
[1] 1072
char_obj <- "hello"
char_obj2 <- "world!"
char_obj3 <- char_obj + char_obj2
Error in char_obj + char_obj2: non-numeric argument to binary operator
Reading the error message is important in R
This error message is essentially telling you that either one or both of the objects char_obj
and char_obj2
is not a number and therefore cannot be added together
Error: object '**' not found
. As an example, take a look at the code below.my_obj <- 48
my_obj4 <- my_obj + no_obj
Error: object 'no_obj' not found
R returns an error message because we haven’t created (defined) the object no_obj
yet.
If you check your environment, you’ll see that object my_obj4
has not been created
In R, object names are case sensitive, and a valid object name (syntactic name) consists of a combination of
(a-z
, A-Z
), (0-9
), (.
), (-
), and (_
)
An object name cannot start with a number or a hyphen or an underscore and can start with a dot, but it must be followed by a letter
A good programming practice is to use meaningful object names in the codes, and self-explanatory object names increase the readability of the codes
Existing R functions, names, or words (e.g., mean
, log
, exp
, TRUE
, c
, etc.) should not be used as object names
x1
which is the number 73
x2
which is the answer to the sum 101+36
x1
and x2
together and store the object as another object called x3
x3
and calculate the 4th root.(
& )
are used to define arithmetic expressions, and they must be matched; unmatched parentheses will result in errors((3 + 12)/3 + 8)
[1] 13
{
& }
do not result in any error but should not be used as they have some specific uses in R, e.g. defining a function{10 + 2}+ 5
[1] 17
[
& ]
cannot be used in arithmetic expressions2 + 7]/3 [
Error in parse(text = input): <text>:1:1: unexpected '['
1: [
^
Description | R symbol | Example |
---|---|---|
square root | sqrt | sqrt(225) |
natural logarithm | log | log(50) |
exponential | exp | exp(3) |
absolute | abs | abs(-10) |
factorial | factorial | factorial(6) |
sine function | sin | sin(25) |
inverse cosine | acos | acos(-8.67) |
function_name(arg1 = val1, arg2 = val2, ...)
Let’s try using sqrt()
sqrt(25)
or sqrt(x = 25)
will return a value 5
( )
even if there’s no input.fun_name(
input1, input2, input3)
(1+2) ^ 3
[1] 27
( 1+ 2) ^ 3
[1] 27
Basically, you can put spaces between different values, and you can put as many as you want and R won’t care.
However, if you insert a space within a single value, R will generate an error.
3 .14
Error in parse(text = input): <text>:1:5: unexpected numeric constant
1: 3 .14
^
R will ignore anything on the same line that follows the #
symbol.
# Calculating BMI
weight <- 70 # This is in kg
height <- 1.7 # This is in meter
bmi <- weight / height ^ 2
bmi
[1] 24.22145
my_variable <- 10
my_varıable
Error: object 'my_varıable' not found
Create an object called myObject
and assign it a value between 1 and 100
Add 13 to myObject
, making sure the object itself stores the updated value
Is myObject
divisible by 2? by 3? by 13? by 21? Use the R code to get the answer.
How many times can 5 fit in myObject
?