In most R functions, you can use named colors, hex, or RGB values. Each column is a vector or factor.Ĭreating a dataframe: dfr1 30,2] # "Jim" "Jane" "Jill"įind the average age of all females in the data: mean ( dfr1 ) # 49.5 Think of rows as cases, columns as variables. The data frame is a special kind of list used for storing dataset tables. l1$Something <- "A thing" # Adds a ninth element - "A thing", named "Something" Elements number 6 and 7 will be created empty (NULL). We added an 8th element, but not 6th and 7th to the list l1 above. l1] <- "More elements!" # The list l1 had 4 elements, we're adding a 5th here.l1] <- 1:11 Since we added element 3 to the list l4above, elements 1 and 2 will be generated and empty (NULL). l1 <- list(boo=v1,foo=v2,moo=v3,zoo="Animals!") # A list with four componentsl2 <- list(v1,v2,v3,"Animals!")Ĭreate an empty list: l3 <- list()l4 <- NULLĪccessing list elements: l1 # Access boo with single brackets: this returns a list.l1] # Access boo with double brackets: this returns the numeric vectorl1] # Returns the first component of the list, equivalent to above.l1$boo # Named elements can be accessed with the $ operator, as with ]Īdding more elements to a list: l3] <- 11 # add an element to the empty list l3l4] <- c(22, 23) # add a vector as element 3 in the empty list l4. The elements of lists are often named for easier access. A single list can contain all kinds of elements - character strings, numeric vectors, matrices, other lists, and so on. We can create them using the array() function: a <- array(data=1:18,dim=c(3,3,2)) # 3d with dimensions 3x3x2a <- array(1:18,c(3,3,2)) # the same array levels() # The levels (distinct values) of the factor (categorical var) # "blue" "brown" "green" as.numeric() # As numeric values: 1 is blue, 2 is brown, 3 is green # 2 3 2 1 1 1 as.numeric() # The character vector can not be coerced to numeric # Warning: NAs introduced by coercion # NA NA NA NA NA NA as.character() # "brown" "green" "brown" "blue" "blue" "blue" as.character() # "brown" "green" "brown" "blue" "blue" "blue"Ī matrix is a vector with dimensions: m 3, FALSE otherwise:m>3 # Selects only TRUE elements - that is ones greater than 3:m t(m) # Transpose m m <- t(m) # Assign m the transposed mm %*% t(m) # %*% does matrix multiplicationm * m # * does element-wise multiplicationĪrrays are used when we have more than 2 dimensions. The data is stored internally as integers - each number corresponding to a factor level. R will identify the different levels of the factor - e.g. all distinct values. <- c("brown", "green", "brown", "blue", "blue", "blue") # <- factor(c("brown", "green", "brown", "blue", "blue", "blue")) # "brown" "green" "brown" "blue" "blue" "blue" # brown green brown blue blue blue # Levels: blue brown green We can also directly assign the vector a length: length(v1) <- 15 # the last 5 elements are added as missing data: NAįactors are used to store categorical data. To add more elements to a vector, simply assign them values. Note that the indexing in R starts from 1, a fact known to confuse and upset people used to languages that index from 0. Vector elements: v1 # third element of v1v1 # elements 2, 3, 4 of v1v1 # elements 1 and 3 - note that your indexes are a vectorv1 # elements 1 and 2 - only the ones that are TRUEv1 # v1>3 is a logical vector TRUE for elements >3
v1 2 # Each element is compared to 2, returns logical vectorv1=v2 # Are corresponding elements equivalent, returns logical vector.v1!=v2 # Are corresponding elements *not* equivalent? Same as !(v1=v2)(v1>2) | (v2>0) # | is the boolean OR, returns a vector.(v1>2) & (v2>0) # & is the boolean AND, returns a vector.(v1>2) || (v2>0) # || is the boolean OR, returns a single value(v1>2) & (v2>0) # & is the boolean AND, ditto Vectors can be constructed by combining their elements with the important R function c().