? |
pull up a help page |
?seq |
: |
colon operator(generate regular sequence) |
1.5:4 |
<- |
assignment |
x <- 5 + 7 |
c |
concatenate |
c(1.1, 9, 3.14) |
seq, seq_along |
sequence generation |
seq(1,10,0.5) |
length |
return the length of a vector |
length(pi:100) |
rep |
replicate elements of vectors |
rep(c(1,2,3),5) |
print |
prints its argument |
print(5:10) |
# |
comment character |
# ignore the rest of line |
class |
return the class of object |
class(pi) |
as.numeric, as.integer,… |
explicit coercion |
as.integer(pi) |
matrix |
create a matrix |
matrix(1:6, nrow=2, ncol=3) |
dim |
return the dimension attribute of object |
dim( matrix(1:6, nrow=2, ncol=3) ) |
cbind, rbind |
create matrix by column-/row- binding vectors |
cbind(1:3,4:6) |
list |
create a list |
list(name=“John”, age=20) |
factor |
create a factor |
factor(“blue”,“green”,levels=c(“red”,“green”,“blue”)) |
is.na, is.nan |
check if the argument is NA (or NaN) |
is.na(as.numeric(“abc”)) |
data.frame |
create a data frame |
data.frame(foo = 1:4, bar = c(T, T, F, F)) |
nrow, ncol |
return the number of rows/columns of object |
nrow(data.frame(foo = 1:4, bar = c(T, T, F, F))) |
names |
names of elements of a vector |
names(x) |
colnames, rownames |
column/row names of a matrix/data frame |
colnames(m) |
paste |
concatenate strings |
paste(“aa”,“bb”,“cc”,sep=“:”) |
sum |
summation of elements in a vector |
sum(c(1,5,12)) |
prod |
product of elements in a vector |
prod(c(3,15,-2)) |
max, min |
maximum/minimum value in a vector |
max(c(23,12,12,3)) |
mean, median |
mean/median of a vector |
mean(c(1,5,1,2,7)) |
sort |
sorting |
sort(c(1,5,1,pi,2,7)) |
var, sd |
sample variance/standard deviation of a vector |
sd(c(1,5,1,3,2)) |
cor |
correlation |
cor(c(1,4,6,1), c(2,5,1,9)) |
which.min, which.max |
first index of minimum/maximum value |
which.min(c(4,1,3,4,1)) |