In Session I of this R workshop, we plan to cover the following:
To return to the list of sessions at the TRIA-Net website, click here
There are many different kinds of software available that you can use so what makes R so special and appealing?
To install R on your computer you can go to the following webpage: https://cran.r-project.org/
For the Windows operating system take the following steps:
Congratulations! You can now run R on your own personal computer.
RStudio is an IDE (integrated development environment) that makes R easier to use and more productive. RStudio combines a set of productivity tools into a single environment including:
To install RStudio on your computer you can go to the following webpage: https://www.rstudio.com/products/rstudio/download/
For the windows operating system take the following steps:
Congratulations! You can now use the RStudio IDE environment to simplify and enhance your experience with R.
The R GUI versions, including RStudio, under Windows and Mac OS X can be opened by double-clicking their icons.
When opening R you should see the following environment:
Alternatively, when opening RStudio you have the following environment:
At first, the RStudio environment may look more confusing to navigate and more complicated, but this user interface is very easy to use and simplifies the R experience.
To close R use the command q(). R will ask you if you want to save the workspace image. You can respond with y (yes) or n (no).
When responding with y, the entire R workspace will be written to a .RData file which can become very large. To avoid saving the R workspace we will discuss the alternative to this by saving and using scripts in R.
There are two ways to set your working directory in R:
setwd(...)
setwd("C:/Users/User Name/Documents/FOLDER") sets the working directory to a folder FOLDER within the Documents directory.Check the current working directory by
getwd()
Return the content of the current working directory by
dir()
Script files are nothing more than text files of the commands that you enter in the R console. Some benefits to using Script files over inputting the commands through the R console are:
To create a script file under the “file” dropdown menu select “New file” followed by “R script”. This should open up a blank document where you can begin your work.
To load a script file, go to “File” from the drop down menu at the R console and choose “Open Script”. Find the file and open it.
To save a script file, simply choose “File” from the drop down menu (when you have the script file open and active) and choose “Save As”.
To add comments to a script file you use the # symbol on the keyboard. For example, let’s look at a function we already discussed the getwd(). Let us add a comment so we remember what the function does.
# Check the current working directory
getwd()
R simply ignores anything after a # sign and does not execute it as a command.
To execute a R script file, use the following command:
source("my_script.R")
where “my_script.R” is the name of the file you wish to execute. Note: if your working directory is not set to the proper pathway where the script file is located then R will return with and error.
To find help for a particular function you can use the command ?function_name or help(function_name). Both commands complete the same task.
Sometimes in R we need to load a library. To load a library, we use the command library("library_name").
When using R, you will need to use various variables to store information. You may need to store information of various data types like character, numbers, logical, Boolean, etc. In R there are many different data types:
To return the type of the data type you can use the command typeof(). For example str <- "hello" typeof(str) [1] "character"
When you want to create vector with more than one element, you should use c() function which means to combine the elements into a vector.
# Create a vector
a <- c(1,2,3,4,5)
# Display the vector
print(a)
[1] 1 2 3 4 5factor() function. The nlevels function gives the count of levels.A matrix is a two-dimensional rectangular data set. It can be created using a vector input to the matrix function.
# Create a matrix.
M = matrix( c('a','b','c',1 ,2 ,3 ), nrow = 2, ncol = 3, byrow = TRUE)
# Display the matrix
print(M)
[,1] [,2] [,3]
[1,] "a" "b" "c"
[2,] 1 2 3Data Frames are created using the data.frame() function.
# Create the data frame.
BMI <- data.frame(
gender = c("Male", "Male","Female"),
height = c(72, 69.5, 62),
weight = c(175, 210, 125),
Age = c(25,38,26)
)
# Display the data frame
print(BMI)
gender height weight Age
1 Male 72 175 25
2 Male 69.5 210 38
3 Female 62 125 26 A list is an R-object which can contain many different types of elements inside it like vectors, functions and even another list inside it.
# Create a list.
list1 <- list(c(2,5,3),21.5,'hello', cos)
# Print the list.
print(list1)
[[1]]
[1] 2 5 3
[[2]]
[1] 21.5
[[3]]
[1] "hello"
[[4]]
function (x) .Primitive("cos")help(), c(), data.frame(), etc.Another common way to store information is in a table.
a <- c("Sometimes","Sometimes","Never","Always","Always","Sometimes","Sometimes","Never")
b <- c("Maybe","Maybe","Yes","Maybe","Maybe","No","Yes","No")
results <- table(a,b)
print(results)
b
a Maybe No Yes
Always 2 0 0
Never 0 1 1
Sometimes 2 1 1