Commands and Functions Flashcards
?
pull up a help page
Ex: ?seq
:
colon operator (generate regular sequence) Ex: 1.5:5
[ ]
subset a vector
Ex: x[c(2, 6, 1, 3)]
[[ ]]
extract an element in a list
Ex: x[[“bar”]]
or extract a column in a data frame
Ex: data_frame[[“age”]]
$
extract an element in a list
Ex: x$bar
or extract a column in a data frame
data_frame$age
assignment
Ex: x
c
concatenate
Ex: c(1.1, 9. 3.14)
seq, seq_along
sequence generation
Ex: seq(1, 10, 0.5)
length
return the length of a vector
Ex: length(pi:100)
rep
replicate elements of vectors
Ex: rep(c(1, 2, 3), 5)
prints its argument
Ex: print(5:10)
#
comment character
Ex: #ignore the rest of the line
class
return the class of an object Ex: class(pi)
as.numeric, as.integer
explicit coercion
Ex: as.integer(pi)
matrix
create a matrix
Ex: matrix(1:6, nrow=2, ncol=3)
dim
return the dimension attribute of object
Ex: dim(matrix(1:6, nrow=2, ncol=3) )
cbind, rbind
create matrix by column-/row- binding vectors
Ex: cbind(1:3, 4:6)
list
create a list
Ex: list(name = “John”, age = 20)
factor
create a factor
Ex: factor(“blue”, “green”, levels = c(“red”, “green”, “blue”) )
is.na, is.nan
check if the argument is NA (or NaN)
Ex: 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
Ex: nrow(data.frame(foo = 1:4, bar = c(T, T, F, F) ) )
names
names of elements of a vector
Ex: names(x)
colnames, rownames
column/row names of a matrix/data frame
Ex: colnames(m)
paste
concatenate strings
Ex: paste(“aa”, “bb”, “cc”, sep = “:”)
sum
summation of elements in a vector
Ex: sum(c(1, 5, 12) )