# Only needed for Excel files
library(readxl) # import Excel
library(writexl) # export Excel
A. df below generally refers to the dataframe or dataset you want to work with, although it occassionally also means “degrees of freedom”
B. var below means variable
| Action | R Commands |
|---|---|
| Set working directory | setwd("C:/yourworkingdirectory") |
| Check working directory | getwd() |
| Open a log file | sink("mylog.txt", split = TRUE) |
| Close log file | sink() |
| Open dataset | load("mydata.RData") |
| Import Excel dataset | df <- read_excel("myfile.xlsx") |
| Import CSV dataset | df <- read.csv("myfile.csv") |
| Import text dataset | df <- read.table("myfile.txt", header=TRUE) |
| Save dataset | save(df, file="mydata.RData") |
| Export to Excel | write_xlsx(df, "myfile.xlsx") |
| Export to CSV | write.csv(df, "myfile.csv", row.names=FALSE) |
| Export to text file | write.table(df, "myfile.txt", row.names=FALSE) |
| Clear data from memory | rm(list=ls()) |
| Delete one variable | df$varname <- NULL |
| List contents of dataset | str(df) |
| Means and standard deviations | summary(df) |
| Detailed summary statistics | summary(df$var); sd(df$var, na.rm=TRUE); quantile(df$var, na.rm=TRUE) |
| List data | head(df) |
| List all objects in your environment | ls() |
| List one variable | df$varname |
| Create new variable | df$newvar <- somevalue |
| Create new variable with values | varname <- c(value, value, value) |
| Recode variable values | df$var[df$var==1] <- 4 |
| Replace all values | df$var <- newvalue |
| Replace values conditionally | df$var[df$var==somevalue] <- newvalue |
| Rename variable | names(df)[names(df)=="oldname"] <- "newname" |
| Remove variable | rm(var) |
| Count missing values | sum(is.na(df$var)) |
| Generate uniform random variable | df$u <- runif(nrow(df)) |
| Generate binary random variable | df$binary <- sample(c(0,1), nrow(df), replace=TRUE) |
| Set number of observations | df <- data.frame(id=1:n) |
| Set seed | set.seed(12345) |
| Histogram (absolute frequency) | hist(df$var) |
| Histogram (relative frequency) | hist(df$var, probability=TRUE) |
| Box plot | boxplot(df$var) |
| Scatter plot | plot(df$x, df$y) |
| Correlation | cor(df$x, df$y, use="complete.obs") |
| Frequency table | table(df$var) |
| Frequency table without labels | table(as.character(df$var)) |
| Frequency table with condition | table(df$var[df$var >= 3]) |
| Cross-tabulation | table(df$var1, df$var2) |
| Cross-tab with row percentages | prop.table(table(df$var1,df$var2), margin=1) |
| Cross-tab with column percentages | prop.table(table(df$var1,df$var2), margin=2) |
| Critical t-value | qt(1-alpha, df) |
| Two-tailed p-value from t | 2*pt(-abs(tstat), df) |
| Right-tailed p-value from t | pt(tstat, df, lower.tail=FALSE) |
| Left-tailed p-value from t | pt(tstat, df) |
| Critical F-value | qf(1-alpha, df1, df2) |
| F-test p-value | pf(fstat, df1, df2, lower.tail=FALSE) |
| Confidence interval for sample mean from summary statistics | mean + c(-1,1)*qt(.975,n-1)*sd/sqrt(n) |
| Confidence interval for sample proportion | prop.test(successes,n)$conf.int |
| Confidence interval for variable mean | t.test(df$var)$conf.int |
| Confidence interval for variable proportion | prop.test(sum(df$var), length(df$var))$conf.int |
| One-sample z-test (known population SD) | (xbar-mu0)/(sigma/sqrt(n)) |
| One-sample t-test from summary statistics | t <- (xbar-mu0)/(s/sqrt(n)) |
| One-sample proportion test from summary statistics | prop.test(x,n,p=p0) |
| One-sample mean test using dataset variable | t.test(df$var, mu=mu0) |
| One-sample proportion test using dataset variable | prop.test(sum(df$var), length(df$var), p=p0) |
| Difference in means test (two vectors) | t.test(df$x, df$y) |
| Difference in means by group | t.test(y ~ group, data=df) |
| Difference in proportions test | prop.test(c(x1,x2), c(n1,n2)) |
| One-way ANOVA | summary(aov(y ~ group, data=df)) |
| Confidence intervals by group after ANOVA | by(df$y, df$group, function(x) t.test(x)$conf.int) |
| Linear regression | summary(lm(y ~ x1 + x2 + x3, data=df)) |
| Calculator | 2 + 3 |
df$var[df$var == 1] <- 4
df$var[df$var == 2] <- 3
df$var[df$var == 3] <- 2
df$var[df$var == 4] <- 1
df$var[df$var < 0] <- NA
prop.table(
table(df$var1, df$var2),
margin = 1
)
model <- lm(
income ~ education + age + female,
data = df
)
summary(model)
model <- aov(
outcome ~ treatment,
data = df
)
summary(model)