Util Functions
getwd() |
Get working directory |
setwd() |
Set working directory |
ls() |
List the objects declared |
rm() |
Remove an object |
attach() |
Add an object to the environment variable list |
detach() |
Remove an object from the environment variable list |
install.packages() |
Install R Packages |
library() |
Load the packages to the library |
Aggregate Function
mean() |
Returns the mean value |
range() |
Returns the range |
sd() |
Returns the standard deviation |
length() |
Returns the length |
String functions
toString() |
Converts variable to string |
sprintf() |
Prints data in specified format |
toupper() |
Convert all characters to uppercase |
tolower() |
Convert all characters to lowercase |
substr() |
Extract a substring |
paste() |
Append two strings with delimiters in between |
Date and Time
Sys.time() |
Returns system date and time |
POSIXlt() |
Date and time in POSIXlt format |
POSIXct() |
Date and time in POSIXct format |
strptime() |
strptime("20180722T093000Z","%Y%m%dT%H%M%SZ", tz="UTC") |
strftime() |
strftime(now_ct, "It's %I:%M%p on %A %d %B, %Y.") |
|
|
Matrix
matrix() |
Used to creates a matrix |
Example |
a_matrix<-matrix(1:12,byrow=TRUE, nrow=4, #ncol=3 works the same dimnames=list( c("one","two","three","four"), c("c1","c2","c3") )) |
dim() |
Returns the dimension of matrix |
nrow() |
Number of rows |
ncols() |
Number of columns |
identical() |
Returns true if same |
rownames() |
Name of the rows |
colnames() |
Name of the columns |
dimnames() |
Name of the dimensions |
cbind() |
Replicate columns |
rbind() |
Replicate rows |
t() |
Transpose |
Control Structure
if else |
if(a>b & a>c){ print("a is greater") }else if(b>c & b>a){ print("b is greater") }else{ print("c is greater") } |
ifelse() |
ifelse(a>b, ifelse(a>c,"a","c"), ifelse(b>c,"b","c")) |
switch() |
(greek=switch( "gamma", alpha=1, beta=sqrt(4), gamma=4 )) |
Stats
summary() |
Returns a summary of the object |
quantile() |
Returns the quartile details |
hist() |
Creates a frequency table |
density() |
Creates a standardized frequency table |
plot() |
Creates a graph |
boxplot() |
Creates a boxplot |
ggplot() |
Visualization function |
qpplot() |
Visualization function |
|
|
Other functions
which.min() |
Returns the minimum from a list |
which.max() |
Returns the maximum from a list |
rep() |
Replicates a vector |
Loop
while |
while(i<10){ print(i) i=i+1 } |
for |
for(i in 1:5) message("i=",i) |
for |
for(month in month.name){ message("The month of ",month) } |
Dataframes
Example |
d=data.frame(subjectID=1:5,gender=c("M","F","F","M","F"), score=c(8,3,6,5,5)) |
Filters |
d[3,1];d[,"subjectID"]>3;d[,c("subjectID","gender")]
d$subjectID
filter=d$subjectID>3
d[filter,]
d["subjectID"];d$subjectID>3;d[d$subjectID>3,]
filter2=d$gender=="F"
d[filter2,]
filter3=d$score>5
d[filter3,]
d[filter2,c("score")]
d[filter2 & filter3,][,c("score")]
subset() - Returns a filtered set of data
Joins
merge() |
merge(a_data_frame,b_data_frame,by="x",all=TRUE) |
colsums() |
colSums(a_data_frame[,-1]) |
colmeans() |
colMeans(fw[,-2]) |
|