If you want to work with R, the first thing you should do is to install R and RStudio on your computer. You can find on the internet all the information about how to install them, and they are free. RStudio can be thought of as a more friendly platform to communicate with R.
RStudio has four windows. The window on the top right has Environment, History, Connections, etc. tabs. The window on the bottom right has the Files, Plots, Packages, etc. tabs. Top left window is known as the Source window. The bottom left window is the Console window.
The Console is the place R evaluates all the code you write. You can type your code directly into the Console at the command line prompt, ">". However, when you start writing more R code, it is better to create an R script that contains the lines of your code, and then execute them from there. To create an R script, click on the "File" menu and then select New File --> R Script (or click "R Script" under icon with the small green plus in the top left corner of RStudio.) To run any line in R Script, go to that line and press "CMND+ENTER".
R is case sensitive i.e. print is not the same as Print. Anything that follows a # symbol is interpreted as a comment and ignored by R. When you type your commands in Console and get a + symbol, it means that you have not completed your code correctly. Writing your code in R Script means that you will have a permanent record of what you did when you save the script.
print("a")
## [1] "a"
Print("a") # R is case sensitive
## Error in Print("a"): could not find function "Print"
2+4/ + 3
## [1] 3.333333
You can use R as a calculator.
2+3/4
## [1] 2.75
log(10)
## [1] 2.302585
exp(3)
## [1] 20.08554
sqrt(16)
## [1] 4
5^2
## [1] 25
pi
## [1] 3.141593
The results of these expressions will be displayed in the Console.However, it is much more useful to store (assign) those results to objects (variables). To create a variable you should choose a name and use the assignment operator <- as follows. Shortcut for it is Command and - keys hold down together.
age <- 53 # 53 is assigned to the variable age name <- "John" # character strings should be inside quotes 2023 - age
## [1] 1970
How about if we want to join together a series of values and store them in a variable. We can use the c() function for this purpose. The object we create to store a series of values is called a vector.
vector_1 <- c(3,1,5,4,6,7,5,1) vector_1 # prints out the value of our object
## [1] 3 1 5 4 6 7 5 1
vec_2 <- c("a", "b", "c", "d", "e") vec_2
## [1] "a" "b" "c" "d" "e"
We can use other functions to do some useful things with a vector.
mean(vector_1) # calculates the mean of our vector
## [1] 4
var(vector_1) # calculates the variance
## [1] 4.857143
sd(vector_1) # calculates the standard deviation
## [1] 2.203893
length(vector_1) # finds the number of element in our vector
## [1] 8
total <- sum(vector_1) n <- length(vector_1) m <- total / n # another way of calculating mean m # m and mean(vector_1) is the same
## [1] 4
We can create a vector of values increasing or decreasing by one using the colon : .
x1 <- 1:10 x1
## [1] 1 2 3 4 5 6 7 8 9 10
x2 <- 12:3 x2
## [1] 12 11 10 9 8 7 6 5 4 3
If change in the pattern is different from one, then using the function seq() will be much more useful.
x1 <- seq(from=4, to=10, by=0.5) x1
## [1] 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0 8.5 9.0 9.5 10.0
x2<- seq(from=3.5, to=7, by=0.25) x2
## [1] 3.50 3.75 4.00 4.25 4.50 4.75 5.00 5.25 5.50 5.75 6.00 6.25 6.50 6.75 7.00
Sometimes we want to create a vector that repeats values a certain number of times. The function rep() allows us to do that.
x1 <- rep(3, times=6) # repeats 3, 6 times x1
## [1] 3 3 3 3 3 3
x2 <- rep("abc", times=4) x2
## [1] "abc" "abc" "abc" "abc"
x3 <- rep(2:7, times=2) x3
## [1] 2 3 4 5 6 7 2 3 4 5 6 7
x4 <- rep(2:7, each=3) x4
## [1] 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7