R programming
What is the operator to extract elements of a list or data frame by name?
$
what will 1:10 print out?
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
What symbol creates a sequence?
:
what is the assignment operator?
<- , assigns a value to an object eg x <- 1
What is the operator to return an object of the same class as the original?
[
which operator would I use to extract multiple items?
[
The --- can be used with computed indices, --- can only be used with literal names
[ ; $
What is the operator to extract elements of a list or data frame.
[[ this can only be used to extract a single element
What is the most basic object?
a vector
How would you convert a vector to a numeric vector?
as.numeric(x) where x is the vector
What function would you use to access an objects attributes?
attributes()
How to remove NA values from a data set x assigning result as bad
bad <- is.na(x) x[!bad]
What function is used to create vectors of objects?
c()
What is the command for column binding to create a matrix?
cbind()
If you have 2 vectors x and y, what command will put them in a matrix where x is in one column and y in another?
cbind(x, y)
Which argument indicates the class of each column
colClasses
which argument is used to indicate the comment character
comment.char
What function would you use to remove NA's to 2 matrix x and y and assign them to good?
complete.cases(x, y)
How to import a file called foo.txt into R
data <- read.table("foo.txt")
what function would you use to convert a data frame to a matrix?
data.matrix()
What calls up the dimensions of a matrix?
dim(x) where x is the matrix
Matrices are vectors with a ...
dimension attribute
What function gives names in a matrix?
dimnames()
How to assign column and row names in a matrix m, so rows are a,b and columns are c,d?
dimnames(m) <- list(c("a", "b"), c("c", "d""))
What is the function to create a factor?
factor()
how to create a factor with character input
factor(c("string 1", "string 2"))
the name of a file or connection
file
What function opens a connection to a remote file?
file()
what are the arguments for the read.table function?
file; header; sep; colClasses; nrows; comment.char; skip; stringsAsFactors
what is the logical argument indicating if the file has a header line
header
inf
infinity
What function is used to test objects if they are NA?
is.na()
What function is used to test objects if they are NaN?
is.nan()
what argument would you use in the factor command to order the levels
levels = c("string 1", "string 2")
How to create a list
list()
How would you create a matrix directly from vectors
m <- 1:10 (assigning m the sequence 1 through 10 dim(m) <- c(2, 5) (puts m into a matrix with 2 rows and 5 columes)
How would you set up a matrix with 3 columes and 2 lines
matrix(nrow = 2, ncol = 3)
How to add column names to a matrix x?
names(x) <- c("name 1", "name 2")
What are the object attributes?
names, dimensions(matrices, arrays), class length, other user defined
How would you get number of columns in the data frame x?
ncol(x)
Can you convert a dataframe with many types of objects into a matrix?
no because matrix's can only contain 1 object type
How would you get number of rows in the data frame x?
nrow(x)
what argument is used to set number of rows?
nrows
A vector can only contain...
objects of the same class
What is the command for row binding to create a matrix?
rbind()
If you have 2 vectors x and y, what command will put them in a matrix where x is in one row and y in another?
rbind(x, y)
What are the 2 functions you can use to create data frames?
read.table() read.csv()
what function is used for reading lines of a text file?
readLines()
What are factors used for?
represent categorical data
Data frames have a special attribute called...
row.names
which argument is a string indicating how columns are seperated
sep
which argument tells R how many lines to skip from the beginning?
skip
data frames are used to...
store tabular data
What argument is used to indicate if character variables should be coded as factors
stringsAsFactors
How to change the factor labels in matrix x to numeric values?
unclass(x)
what function do you use to open a connection to awebpage?
url()
How to create a numeric vector with 10 numbers?
vector("numeric", length=10)
What is the function to create an empty vector?
vector()
what are the analogous functions for writing data to files?
write.table; writeLines; dump; dput; save; serialize
How would you create a data frame x where column foo is numbers 1 through 4 and the next column bar is true, false?
x <- data.frame(foo = 1:4, bar = c(T, T, F, F))
How to give list x names a and b for objects 1 and 2?
x <- list(a = 1, b = 2)
If I have a list x and want to extract the column bar what do I type?
x$bar
How would you extract the first 4 elements of a data list x using a numeric index?
x[1:4]
in matrix x, if I want to extract the first and third objects i'd type...
x[c(1, 3)]
After removing NA's from objects x and y, how would you print them out?
x[good] y[good]
How would you extract elements of a data list x using a logical operator?
x[x > "a"]