

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Essential Notions in this Base R cheat sheet
Typology: Cheat Sheet
1 / 2
This page cannot be seen from the preview
Don't miss anything!
RStudio® is a trademark of RStudio, Inc. • CC BY Mhairi McNeill • mhairihmcneill@gmail.com Learn more at web page or vignette • package version • Updated: 3/1 5 Input Ouput Description df <- read.table(‘file.txt’) write.table(df, ‘file.txt’) Read and write a delimited text file. df <- read.csv(‘file.csv’) write.csv(df, ‘file.csv’) Read and write a comma separated value file. This is a special case of read.table/ write.table. load(‘file.RData’) save(df, file = ’file.Rdata’) Read and write an R data file, a file type special for R.
Accessing the help files More about an object
Programming For Loop for (variable in sequence){ Do something }
for (i in 1:4){ j <- i + 10 print(j) } While Loop while (condition){ Do something }
while (i < 5){ print(i) i <- i + 1 } If Statements if (condition){ Do something } else { Do something different }
if (i > 3){ print(‘Yes’) } else { print(‘No’) } Functions function_name <- function(var){ Do something return(new_variable) }
square <- function(x){ squared <- x*x return(squared) } a == b (^) Are equal a > b Greater than a >= b Greater than or equal to is.na(a)^ Is missing a != b (^) Not equal a < b (^) Less than a <= b Less than or equal to is.null(a)^ Is null Conditions Creating Vectors c(2, 4, 6) 2 4 6 Join elements into a vector 2:6 2 3 4 5 6 An integer sequence seq(2, 3, by=0.5) 2.0 2.5 3.0 A complex sequence rep(1:2, times=3) 1 2 1 2 1 2 Repeat a vector rep(1:2, each=3) 1 1 1 2 2 2 Repeat elements of a vector
Download and install a package from CRAN.
Load the package into the session, making all its functions available to use.
Use a particular function from a package.
Load a built-in dataset into the environment. Vectors Selecting Vector Elements
Reading and Writing Data
Find the current working directory (where inputs are found and outputs are sent).
Change the current working directory.
Vector Functions
RStudio® is a trademark of RStudio, Inc. • CC BY Mhairi McNeill • mhairihmcneill@gmail.com • 844-448-1212 • rstudio.com Learn more at web page or vignette • package version • Updated: 3/1 5 Lists Matrixes Data Frames Maths Functions Types (^) Strings Factors Statistics Distributions as.logical TRUE, FALSE, TRUE Boolean values (TRUE or FALSE). as.numeric 1, 0, 1 Integers or floating point numbers. as.character '1', '0', '1' Character strings. Generally preferred to factors. as.factor '1', '0', '1', levels: '1', '0' Character strings with preset levels. Needed for some statistical models. Converting between common data types in R. Can always go from a higher value in the table to a lower value.
The Environment Variable Assignment
ww ww ww
ww ww ww ww ww ww
log(x) (^) Natural log. sum(x) (^) Sum. exp(x) (^) Exponential. mean(x) (^) Mean. max(x) (^) Largest element. median(x) (^) Median. min(x) (^) Smallest element. quantile(x) (^) Percentage quantiles. round(x, n) (^) Round to n decimal places. rank(x) (^) Rank of elements. signif(x, n) (^) Round to n significant figures. var(x) (^) The variance. cor(x, y) (^) Correlation. sd(x) (^) The standard deviation.
View(df)
head(df)
Plotting Dates See the^ lubridate^ library.
paste(x, y, sep = ' ') (^) Join multiple vectors together. paste(x, collapse = ' ') (^) Join elements of a vector together. grep(pattern, x) (^) Find regular expression matches in x. gsub(pattern, replace, x) (^) Replace matches in x with a string. toupper(x) (^) Convert to uppercase. tolower(x) (^) Convert to lowercase. nchar(x) (^) Number of characters in a string.