R Tutorial
R Tutorial
The left pannel is the console where I can type R codes and run it by pressing enter.
On the top right there is environment which shows all the variables that are being used. This is
where you can check if I preformed the code correctly.
Bottom right are the tabs on the laptop where I can upload files from in R. Clicking on plot will show
the last plot I made.
When opening a file click on the green + on the top left corner and choose R script; if you click on the
green + with R it will open a new project.
Chapter 2 reporting in Rmarkdown
Exercise 1 =
# = main title
## = second title
My creations = https://dillinger.io/
1. Using codes
First to create a markdown you click on the green + and choose markdown. A code always starts
with
“ ‘{r}
The code
“‘
Example 1 =
Importing data = file -> import data -> copy code in R markdown file
Chapter 3 Basic R: Operations, functions and variables
Variables = name, type and value
1. Name = it cannot start with a number and not all characters are allowed. No abbreviations
unless these are incredibly common abbreviations.
- Dot separation: miles.per.gallon
- Camel case: milesPerGallon
- Snake case: miles_per_gallon
So a variable like 2pairs is not possible since it starts with a number. However, two.pairs is possible.
Two pairs is not possible cause spaces should be filled with _.
2. Type = Data types restrict the amount of information a variable can contain. A logical
variable (also called boolean) has two options: TRUE and FALSE. Every value type has
restrictions on the kind of contents it allows for. You will encounter the following, from strict
to lenient.
- Logical, TRUE or FALSE (equivalent of 0 and 1)
- Integer, any whole number (no decimals allowed)
- Numeric, any number (decimals allowed)
- Character, any character allowed.
3. Value = The value has to fit within the possible values that the data type can take
R=
Overwriting a value = so if I first did animal <- cat and then do animal <- dog
the first value is overwritten
Rstudio =
I can also do this in Rstudio, when typing in the script miles_per_gallon <- 6.4 and then select the
line and enter run it will show this;
Exercise 2 operations
Mathematical operators
+ addition
- subtraction
* multiplication
/ division
ˆ or ** exponentiation
%% modulo
%/% floor division (division result without decimals, always rounded down)
Example 1 =
First number = 47
Print(first_number + second_number)
Logical operators
== for equality
!= for difference
Example 2 =
Example 3
Exercise 3 functions
Function signature =
Structure function =
# code
return(variable) }
Example 1 =
return(number ** .5) }
Then when I enter square_root and fill in as a number 5 it will give the square root of 5
Exercise 4 R internal help system
?function_name
??search_term
This opens the help file with possible solutions. Other possibilities are the internet.
Chapter 4 Vectors, list, and data frames
Vectors
Making a vector = c(…)
- Heights <- c(1,92, 1,87, 1,63, 1,84)
- Names(height) <-c(“Max, “Bob”, “Rose”,
“Emily”)
Index
When coding print(heights[3]) R automatically gives the third value 1.63
Slice = [first_index:last_indext]
Changing values = number[2] <- 9
Example 1 =
Created a new vector with additional names 7 and TRUE = this vector contains a number instead of a
word.
List
There are three ways to retrieve elements from a list. Depending on (a) the type of list and (b) the
type of output you expect, you can choose from the following.
Example 1 =
Dataframe
Adding columns
Example 1 =
Example 1 =
How to find out if numbers are odd or even. First add the variable and its values. Then say numbers
logical when the number %%2 = 0. Then say numbers even and numbers odd! It should give the
numbers that are odd and even.
numbers_odd[1:3]<-c(4,6,8) = this helps to add 1 to all the odd numbers but it isn’t done with the if
else function…
Extra functions
%in% can show whether an something is part of something else, which looks for where elements
with certain characteristics are.
print(8 %in% c(1, 3, 5, 7, 9, 11)) # FALSE -> 8 is not part of the range
Example 1=
which(fruit_basket == "4") = tried this to find out what on position 4 is but didn’t work out