Show Menu
Cheatography

R Cheatsheet Cheat Sheet (DRAFT) by

A cheatsheet for TariSauce

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Combine multiple .txt files

//create a list of all.txt files in directory
list.txt<-dir(pattern = "*.txt")

//create an empty list
ldf<-list()

//for each file, load in, rename columns and add to ldf
for (i in 1:length(listtxt)){

   // read each text file in
   ldf[[i]] <- (read.delim(listtxt[i]))

   //rename column names so all are the same
   colnames(ldf[[i]]) <- c("Selection","View", "Channel", "Begin_Time", "End_Time",  "Low_Freq",
                           "High_Freq", "Begin_Date", "Begin_Clock",   "End_Clock", "Delta_Time","Notes")
 }

//bind all files into one table
df<-do.call("rbind", ldf)

Combine multiple .csv files

//find any file that starts with file_ and extension .csv in current directory and store name of each file in a vector files
files<- list.files(pattern = "file_.*csv")

//read each file in files into a dataframe with read_csv() storing frames in df_list
df_list<-lapply(files, read_csv)

//concatenate all dataframes together
combine<-bind_rows(df_list)

Set up and Loading Data

set working directory
setwd(­"file directory path")
load packages
librar­y(p­ack­age.name)
import .csv file
data<-­rea­d.c­sv(­"­fil­ena­me.c­sv­", fileEn­cod­ing­="UT­F-8­-BO­M")
import excel file
data<-­rea­d.x­lsx­(ch­oos­e.f­ile­s("f­ile.pa­th", sheetN­ame­="fi­le.n­am­e")

Viewing and Basic Functions

view first 5 rows
head(data)
view last 5 rows
tail(data)
show dataframe structure
str(data)
mean(d­ata­$co­lumn)
print mean of column
median­(da­ta$­column)
print median of column
var(da­ta$­column)
print variance of column
sd(dat­a$c­olumn
print standard deviation of column