Skip to content

caseycesari/r-cheat-sheet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 

Repository files navigation

##R Cheat Sheet

Turn off scientific notation except for very large numbers

options(scipen=20)

source

###Data transformation

Make a new date column from a column with datetime data stored as a string

data$date <- as.POSIXct(data$date, format='%m/%d/%Y')

or

data$date <- as.Date(data$date, format="%m/%d/%y")

source source

Convert a column containing a string of UNIX time to a date

date[, {column number}] <- as.Date(as.POSIXct(data[, {column number}], origin='1970-01-01'))

Group and sum a column into a new dataframe

# Assuming a following data frame structured as data[store, revenue, date]

new <- aggregate(data$revenue, by=list(date=data$date), FUN=sum)

source

Subset a dataframe by a date range

subset <- subset(data, date >= '2000-01-01' & date <= '2010-01-01')

source

Subset a dataframe by regex matching

subset <- subset(data, grepl("regex", data[[column number]]))

source

Rename dataframe column names

colnames(date) <- c("column 1 name", "column 2 name", "column 3 name")

source

Sort a dataframe

attach(data)
sorted <- data[order(data$sort_column) , ] 
# or, in decending order
sorted <- data[order(-data$sort_column) , ]
detach(data)

source

###Analysis

Generate a table of frequencies for the values in a single column

count_of_value <- as.data.frame(table(data["column"]))

source

###Graphing

Make a timeseries line chart by group

# Assuming a following data frame structured as data[store, revenue, date]
# and library("ggplot2")

qplot(date, revenue, data=data, group=store, geom="line")

source

Turn off axes, and add your own customized verions

plot(data, axes=FALSE)

# See ?axis for options
axis(...)

source

###Mapping

Amanda Cox's R Training from #NICAR13

About

My cheat sheet for R

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published