Utility Functionsgetwd() | Find the working dir. | setwd("C:/file/path") | Set the working dir. | ls() | List variables | rm(object) | Delete object | str(object) | Displays internal structure of R object | help.start() | Launch help console | install.packages("package_name") | Install package | library(package_name) | Load package | detach(package:package_name) | Remove package | scan() | Read data values |
Listslist(x=1:5, y=c('a', 'b')) | Create list | is.list() Check if the arg is a list | as.list() Force the arg to list | lapply(list_name, function) Apply function over a list and return as list | sapply(list_name, function) Return as suitable data structure(vector) |
Stringsc("String1","String2") | Create a string vector | toString(x) | Convert to string | noqutoe(string) | Print string w/o quote | sprintf() | Print text & var values | cat() | Concatenate & print | toupper(string) | Convert to uppercase | tolower(string) | Convert to lowercase | substr(string,n,m) | Extract substrings in a string from n to m | strsplit(string," ") | Split elements of string | paste(c("a","b"),"c") Concatenate vectors | paste0(c("a","b"),"c") Concat w/o separator |
Probability Distributionsrbinom(n, size, prob) | Binomial | rpois(n, lambda) | Poisson | runif(n, min=0, max=1) | Uniform | rnorm(n, mean=0, sd=1) | Normal | rexp(n, rate=1) | Exponential |
| | Vectorsc(2, 4, 6) | Numeric vector | c("one","two","thr") | Character vector | c(TRUE,FALSE) | Logical vector | rep(1:2,times=3) | Repeat a vector | rep(1:2,each=3) | Repeat the elements | which.min() Index of the min | which.max() Index of the max |
Data Framesdata.frame(x=1:2, y=c('a', 'b')) | Create data frame | View(df_name) | See full dataframe | head(df_name) | See first 6 rows | tail(df_name) | See last 6 rows | df_name[cond, ] | Row filter | df_name[c("column")] | Column filter | df_name[cond1, ][ ,cond2] Row and Column filter |
Functionsfunction_name <- function(var){
Do something
return(new_variable)}
|
args(function_name) - Arguments of func
body(function_name) - Body of func
Flow ControlIf Statement -
if (condition){
Do something
} else {
Do something different}
-------------------------------
Ifelse Statement -
ifelse(condition, Do something, Do something different)
-------------------------------
Switch Statement -
switch("beta","alpha=1,beta=2,gamma=3,4)
|
LoopsFor Loop -
for (var in sequence){Do something} | While Loop -
while (condition){ Do something} |
Visualizationsbarplot() | plot() | qqnorm() | pie() | plot(density()) | qplot() | mosaicplot() | pairs() | boxplot() | hist() | matplot() | ggplot() |
| | Arraysarray(1:24, dim=c(4,3,2), dimnames=......)
Create array with 4 rows, 3 cols and 2 groups |
Matricesm1 <- matrix(1:12, now=4, ncol=3, dimnames=....) Create a matrix with 4 rows and 3 columns | t(m) | Transpose of matrix | rbind(m1,m2) Combine by row | cbind(m1,m2) Combine by column | The following applies to arrays also: | dimnames(m) | dim(m) | colnames(m) | rownames(m) | nrow(m) | ncol(m) |
Descriptive Statisticssummary(object) | Summary of object | class(object) | Find class of an R object | length(object) | Get length of an object | quantile(x) | Find quantiles | rowMeans(x)/ colMeans(x) | rowSums(x)/ colSums(x) | table(x) | Build a contingency table | describe(object) | Description of object | subset(x,cond) | Create subsets |
Hypothesis Testingt.test(data, mu=3) One sample two-sided t-test | t.test(data, mu=3, alternative='greater') One sample one-sided t-test | t.test(data1, data2, mu=0.5) Two sample two-sided t-test | t.test(data1, data2, mu=0.5, alternative='less') Two sample one-sided t-test | t.test(post_data, pre_data, paired=TRUE) Paired test | wilcox.test(data, mu=8, alt='less') Wilcoxen test | cor.test(data1, data2) Correlation test | chisq.test(data) Chi-square test | ks.test(data1, data2) If both are frm same distn | shapiro.test(data) Normality test | aov(data1~ data2) ANOVA | lm(data1~ data2) Regression |
|
Created By
Metadata
Comments
No comments yet. Add yours below!
Add a Comment
Related Cheat Sheets