{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

5-ListsAndApply

The former returns a list object and the laner a

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 12 4 Summary of Data Structures Types of structures •  To summarize, the data structures we have encountered so far are: –  vector –  data frame –  list –  matrix Vector – Data frame - List Vector Data Frame List Vector Data frame Matrix Ordered collec9on of primi9ve Ordered collec9on of vectors all same length types Ordered collec9on of objects Indexing data structures •  Vectors: [index] > x[1:10] > x[-3] > x[x>3] •  Data frames: [rowindex, colindex] and \$name > family\$weight > family[, 3:4] Returns a vector > family[famiy\$height > 70, 2] when possible, unless use drop = FALSE •  Note: both \$ can index only one element. Indexing data structures •  > > > Lists: \$name, [index], [[index]] rain\$stationname ! rain[1:2] ! rain[[1]]! •  Matrices: [rowindex, colindex] > m[1, 2] ! > m[1:2, ]! > m[ ,“a”, drop = TRUE]! •  Note: [[ ]] can index only one element. Also we can index a matrix as if it were a vector Apply Func9ons The “Apply” Func9on •  Some9mes we want an opera9on to be applied to each element of a list, to each vector in a data frame, or to individual dimensions of a matrix •  R provides the apply mechanism to do this. •  There are several apply func9ons: –  sapply() and lapply() for lists and data frames –  apply() for matrices –  tapply() for “tables”, i.e. ragged arrays as vectors •  With these func9ons we can avoid looping, and instead write code that is meaningful in a sta9s9cal sekng. •  For example with our list of rainfall data, each element represents the measurements taken at a par9cular weather sta9on and when we think about studying the average rainfall at each sta9on - we don’t think in terms of loops. Rainfall •  Daily rainfall collected...
View Full Document

{[ snackBarMessage ]}