R Code.A list of useful R code for Master, PhD and Post Graduate
|
List of R Code Used by E3B Graduate Students |
Basic R Commands:
Creating vectors, matrices, and data frames:
Manipulating data sets:
Creating functions:
- install.packages("package_name"): used to install a package into R, only needs to be done once
- library(package_name): used to run installed packages into R, needs to be run every time you open R
- search(): used to see a list of libraries currently loaded
- help(command_name): used to request more information on how to use certain commands
Creating vectors, matrices, and data frames:
- vector_name <- c(vector_input): used to create a vector
- x <- c(1:10) will create a vector of integers ranging from 1 to 10
- x <- c(1:10, 15, 20) will create a vector of integers ranging from 1 to 10 as well as the values 15 and 20
- x <- c(1:10, 15:20) will create a vector of integers ranging from 1 to 10 as well as the values 15 to 20
- x <- c(1, 5, by=0.5) will create a vector of integers ranging from 1 to 5 broken by 0.5 (i.e. 1.0, 1.5, 2.0, 2.5...5.0)
- x <- rep(1:3, 5) will create a vector consisting of integers ranging from 1 to 3 repeated 5 times
- Calculations can also be saved as vectors such as vector <- data*pi
- x <- c(1:10) will create a vector of integers ranging from 1 to 10
- matrix_name <- cbind(matrix_input): used to create a matrix consisting of two vectors; helpful for combining rows (rbind) and columns (cbind)
- dataframe_name <- read.table("file_name.txt"): used to import data tables into R from txt files into a data frame. To read csv files, use read.csv("file_name.csv")
- Tip: prior to converting an excel file to a txt file, remove all spaces from cells so that "cell 1" becomes "cell1", "cell_1", or something similar since each space in an txt file will be considered the start of a new cell, which can cause the data to become misaligned when read through R
- Similarly, do not keep any cells empty and instead place "NA" in them. These can be removed from the data frame by using the command read.table("file_name.txt", na.strings = c("n/a", "NA"))
- To set the first column of the data set as the header of the data frame, use read.table("file_name.txt", header = TRUE
- Tip: prior to converting an excel file to a txt file, remove all spaces from cells so that "cell 1" becomes "cell1", "cell_1", or something similar since each space in an txt file will be considered the start of a new cell, which can cause the data to become misaligned when read through R
Manipulating data sets:
- data$column_name: used to call a certain column from the data set
- To select multiple columns, you can use data[, c(1, 3, 5)] to specify column numbers or data[, c("column1", "column3", "column5")] to specify column names
- data[data == 0] <- NA: used to replace all 0 values in a data set with NA
- To remove all rows containing NA values, use data2 <- data[is.na(data) == FALSE, ]
- data[1:4,]: used to rows 1 to 4 from a data set
- summary(data): used to get the summary statistics of a data set (Min, Max, Median, Mean, Quantiles)
- Individual summary statistics can be calculated using mean(), min(), max(), sd(), var(), etc.
- As NA values can cause errors during calculations, use the command na.rm = TRUE within the parentheses such as mean(data, na.rm=TRUE) to remove them from the calculations
- Individual summary statistics can be calculated using mean(), min(), max(), sd(), var(), etc.
- data[data==1]: used to specify rows where there is a value of 1. To search for words, use data[data=="one"]
- To search in specific columns, use data[data$column_name==1]
- Other searching operations include > (greater than), >= (greater or equal to), < (less than), <= (less or equal to), ~~ (contain), != (does not equal), !~ (does not contain)
- To include multiple search criteria, use either & (and) or | (or) to search for rows containing both or either values, respectively. The former could only be used if two or more columns are present.
Creating functions:
- function_name <- function(x){function equation}
- Other variables can be included with x such as a, b, alpha, beta, etc. as long as each variable is defined such as function(x, t=30, delta=5)
- Make sure that all variables appear in the function equation or else an error would occur
- ex. function_name <- function(x, c=15, theta=0.5){x*c^theta}
-