Util functions
getwd() |
Get working directory |
setwd() |
Set a path in working directory |
ls() |
list the memory content |
rm() |
remove variable from memory |
install.packages() |
Installs the required package |
library() |
Makes the package available for usage |
str() |
shows the structure of the variable |
detach() |
removes the package |
history() |
displays the executed commands in the session |
help.start() |
opens the index page of R |
Data Frame
data_frame = data.frame(subjectID=1:5,gender=c("M","F","M","M","F"),score=c(8,3,6,5,5)) |
Data frame with elements of equal length |
view(data_frame) |
opens the editor |
rbind(data_frame1,data_frame2) |
combines two data frames vertically/row wise |
cbind(data_frame1,data_frame2) |
combines two data frames horizontally/column wise |
head(data_frame)/tail(data_frame) |
returns the first / last part of the data frame |
summary(data_frame) |
returns the descriptive statistics |
Strings
toString(x) |
produces a single character string |
tolower() |
converts text to lower case |
toupper() |
converts text to upper case |
substr() |
extract or replace a substring in a character vector |
paste (…, sep = " ", collapse = NULL) |
concatenate vectors after character conversion |
|
|
Arrays & Matrices
one_d_array<-array(1:15) |
one dimensional array |
two_d_array<-array(1:15,dim=c(3,4)) |
two dimensional array |
3_d_array<-array(1:15,dim=c(3,4,2)) |
three dimensional array |
mat1=matrix(1:15,nrow=5,ncol=3) |
creates a 5x3 matrix |
cbind(mat1,mat2)/rbind(mat1,mat2) |
column bind/row bind |
solve(mat1)%*%mat1 |
Inverse of a matrix |
det(matrix(c(1,0,0,1),2) |
determinant of a matrix |
Vectors
scores<-c(3,4,6,7,5,8,6) |
numerical vector |
names<-c("Nancy","Selvarani") |
character Vector |
x<-c(TRUE,TRUE,FALSE) |
logical vector |
mean(scores) |
mean of the score vector |
sd(Scores) |
standard deviation of score vector |
var(scores) |
variance of the vector |
range(scores) |
range of the vector |
which.min(vector)/which.max(vector) |
returns the position of the min/max value |
rep(1:4,times=2) |
replicates the elements of the vector twice |
Lists
list<-list(c("Nancy", "selvarani","Aravind"),c(25,23,26)) |
creates list with elements of diff data types |
names(list)<-c("Names","Age") |
names the elements in the list |
Descriptive Statistics
summary(mydata) |
description of mydata |
rowMeans(mydata[]) |
returns the row mean value |
colSums(mydata[]) |
returns the column sum |
|
|
Hypothesis Testing
t.test(data1) |
one sample t test |
t.test(data1,data2) |
two sample t test |
t.test(pre,post,paired=TRUE) |
paired t test |
prop.test |
test for diff between 2 proportions |
cor.test(data1,data2) |
correlation |
wilcox.test(data1) |
Wilcox test |
chisq.test(data1) |
chi square test |
shapiro.test(data1) |
test for normality |
aov() |
ANOVA |
Visualization
ggplot(data = NULL, mapping = aes(), ...) |
initializes the plot object |
qplot(data, line=TRUE,...) |
quantile-quantile plot |
geom_density() |
density plot |
geom_hist() |
histogram |
geom_point() |
Scatter plots |
geom_bar() |
Bar graph |
facet_grid() |
panel layout in a grid |
barplot(list) |
bar plot |
Probability
rnorm(n,mean,sd) |
normal distribution |
runif() |
uniform distribution |
rpois(n,size) |
poisson distribution |
rbinom(n,size,prob) |
Binomial distribution |
rexp(n) |
Exponential distribution |
Statistics
lm(y~x,data=mydata) |
linear regression |
summary(lm(y ~ x1 + x2 + x3, data=mydata)) |
multiple regression |
summary(glm(y ~ x1 + x2 + x3, family="", data=mydata)) |
classification |
predict(object,mydata) |
Regression model |
cl$cluster |
Clustering |
cluster=kmeans(mydata) |
K means cluster analysis |
|