print("Hello World!")
[1] "Hello World!"
(AST230) R for Data Science
print("Hello World!")
[1] "Hello World!"
2+4
[1] 6
59 + 73 + 2) / 3 (
[1] 44.66667
You don’t need to type an equals sign, just hit enter.
Arithmetic operators are the same as they are in most other computer applications.
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.
<- 48 my_obj
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
<- "R is cool" my_obj2
my_obj2
and assigned it a value of R is cool
which is a character string.<- R is cool my_obj2
Error: <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
<- 1024 my_obj2
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_obj + my_obj2
my_obj3 my_obj3
[1] 1072
<- "hello"
char_obj <- "world!"
char_obj2 <- char_obj + char_obj2 char_obj3
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.<- 48
my_obj <- my_obj + no_obj my_obj4
Error in eval(expr, envir, enclos): 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 73x2
which is the answer to the sum 101+36x1
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 errors3 + 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 function10 + 2}+ 5 {
[1] 17
[
& ]
cannot be used in arithmetic expressions2 + 7]/3 [
Error: <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)
round(3.567, digits=2)
[1] 3.57
floor(3.567)
[1] 3
ceiling(3.567)
[1] 4
# Not a function, but useful pi
[1] 3.141593
<- 10
my_variable my_varıable
Error in eval(expr, envir, enclos): 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
?