R & RSTUDIO
HERHALING R & RSTUDIO
ELEANOR YILDIRIM
2022-2023
,BASICS
Basic math
Introduction
Some useful shortcuts:
CTRL+ENTER Executes on the command line the script line containing the cursor
Up Arrow [In console] Cycles through previously entered commands
TAB Activates autocomplete - very useful for functions and variable names
ESC Interrupt currently executing command - very useful when R gets stuck
First small exercise
R understands the four arithmetic functions: +, -, *, /, as well as exponents via ^
Look at this example of basic math operations.
5+4 # add numbers
2^3 # raise a number to an exponent
1. Add 6 and 3
2. Subtract 5 from 9
3. Divide 12 by 3
4. Multiply 20 by 2
5. Take the second power of 7
1. 6+3
2. 9-5
3. 12/3
4. 20*2
5. 7^2
[1] 9
[1] 4
[1] 4
[1] 40
[1] 49
1
, Creating objects
Introduction
When you assign an object to a certain name, you use an arrow. See in the example below
how a value is assigned to an object by using an arrow that points at the object.
object
object <- <- "value"
"value"
Now the value is assigned to the object, but the object will not be shown in the output. To
view an object, simply type in the name of the object and run the line. (You can try this
yourself in the exercises.)
Objects
There are different types of objects we can define:
1. Numeric object
2. Character object
3. Vector object
1. Numeric object
A numeric object represents a number. See in the example how the value 6 gets assigned to
letter x. The object can also be defined by a word, such as 'length'.
x <- 6
length <- 170
2. Character object
A character object represents a letter or word. When we assign a letter/word to an object, we write
quotation marks (") around the letter/word. See in the example how "Thomas" is assigned to name.
name
name<- <-
"Thomas"
"Thomas"
3. Vector object
Vectors are a collection of data of the same type (character, numbers, ...). We can use the
c(...) command to create vectors.
The following example creates a vector consisting of the names of the days of a workweek.
Days_of_a_workweek <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
You can’t have spaces in the name of your object. Therefore, you can use for example
underscores, dots, a hyphen, …
2
, Class
You can check the class of an object by using the class function, see the following example.
1. Class(length)
2. Class(name)
The first line of code will return "numeric" as output and the second line will return
“character” as output.
Exercise 1
1. Assign the number 24 to an object called 'Hours in a day'.
2. Create an object called 'Birth month' and assign to this the name of your birth month.
3. Create a vector with the names of the days of the weekend.
4. Find the class of your weekend vector.
1. Hours_in_a_day <- 24
Hours_in_a_day
2. Birth_month <- "May"
Birth_month
3. Days_of_the_weekend <- c("Saturday", "Sunday")
Days_of_the_weekend
4. class(Days_of_the_weekend)
[1] 24
[1] "May"
[1] "Saturday" "Sunday"
[1] "character"
Exercise 2
The new pop-up store is selling bracelets. They have 5 types of bracelets, with each a
different price and different quantity available. You can find the price and quantity in the
table below.
Bracelet Price Quantity
A 5 100
B 10 30
C 12 2000
D 8 40
E 15 800
3
HERHALING R & RSTUDIO
ELEANOR YILDIRIM
2022-2023
,BASICS
Basic math
Introduction
Some useful shortcuts:
CTRL+ENTER Executes on the command line the script line containing the cursor
Up Arrow [In console] Cycles through previously entered commands
TAB Activates autocomplete - very useful for functions and variable names
ESC Interrupt currently executing command - very useful when R gets stuck
First small exercise
R understands the four arithmetic functions: +, -, *, /, as well as exponents via ^
Look at this example of basic math operations.
5+4 # add numbers
2^3 # raise a number to an exponent
1. Add 6 and 3
2. Subtract 5 from 9
3. Divide 12 by 3
4. Multiply 20 by 2
5. Take the second power of 7
1. 6+3
2. 9-5
3. 12/3
4. 20*2
5. 7^2
[1] 9
[1] 4
[1] 4
[1] 40
[1] 49
1
, Creating objects
Introduction
When you assign an object to a certain name, you use an arrow. See in the example below
how a value is assigned to an object by using an arrow that points at the object.
object
object <- <- "value"
"value"
Now the value is assigned to the object, but the object will not be shown in the output. To
view an object, simply type in the name of the object and run the line. (You can try this
yourself in the exercises.)
Objects
There are different types of objects we can define:
1. Numeric object
2. Character object
3. Vector object
1. Numeric object
A numeric object represents a number. See in the example how the value 6 gets assigned to
letter x. The object can also be defined by a word, such as 'length'.
x <- 6
length <- 170
2. Character object
A character object represents a letter or word. When we assign a letter/word to an object, we write
quotation marks (") around the letter/word. See in the example how "Thomas" is assigned to name.
name
name<- <-
"Thomas"
"Thomas"
3. Vector object
Vectors are a collection of data of the same type (character, numbers, ...). We can use the
c(...) command to create vectors.
The following example creates a vector consisting of the names of the days of a workweek.
Days_of_a_workweek <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
You can’t have spaces in the name of your object. Therefore, you can use for example
underscores, dots, a hyphen, …
2
, Class
You can check the class of an object by using the class function, see the following example.
1. Class(length)
2. Class(name)
The first line of code will return "numeric" as output and the second line will return
“character” as output.
Exercise 1
1. Assign the number 24 to an object called 'Hours in a day'.
2. Create an object called 'Birth month' and assign to this the name of your birth month.
3. Create a vector with the names of the days of the weekend.
4. Find the class of your weekend vector.
1. Hours_in_a_day <- 24
Hours_in_a_day
2. Birth_month <- "May"
Birth_month
3. Days_of_the_weekend <- c("Saturday", "Sunday")
Days_of_the_weekend
4. class(Days_of_the_weekend)
[1] 24
[1] "May"
[1] "Saturday" "Sunday"
[1] "character"
Exercise 2
The new pop-up store is selling bracelets. They have 5 types of bracelets, with each a
different price and different quantity available. You can find the price and quantity in the
table below.
Bracelet Price Quantity
A 5 100
B 10 30
C 12 2000
D 8 40
E 15 800
3