print("Hello World!")
[1] "Hello World!"
(AST230) R for Data Science
Note:
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:
Notice
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.
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
and assigned it a value of R is cool
which is a character string.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
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)
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.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 errors{
& }
do not result in any error but should not be used as they have some specific uses in R, e.g. defining a function[
& ]
cannot be used in arithmetic expressionsDescription | 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) |
Let’s try using sqrt()
sqrt(25)
or sqrt(x = 25)
will return a value 5
Note
( )
even if there’s no input.fun_name(
input1, input2, input3)
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
?