[1] 3
POP88162 Introduction to Quantitative Research Methods
Department of Political Science, Trinity College Dublin
<- is the standard assignment operator in R= is also supported it is not recommendedBase R data structures can be classified along their:
5 main built-in data structures in R:
vector)matrix)array)list)data.frame)| Structure | Description | Dimensionality | Data Type |
|---|---|---|---|
vector |
Atomic vector (scalar) | 1d | homogenous |
matrix |
Matrix | 2d | homogenous |
array |
One-, two or n-dimensional array | 1d/2d/nd | homogenous |
list |
List | 1d | heterogeneous |
data.frame |
Rectangular data | 2d | heterogeneous |
c() function (short for combine)4 common data types that are contained in R structures:
character)integer)double/numeric)logical)[1] 300 200 4 0 0 1
NA - value exists, but is unknown (e.g. survey non-response)NULL - object does not existExtra
[] to index the elements you would like to select:vector[index]
| Value | Example | Description |
|---|---|---|
| Positive integers | v[c(3, 1)] |
Returns elements at specified positions |
| Negative integers | v[-c(3, 1)] |
Omits elements at specified positions |
| Logical vectors | v[c(FALSE, TRUE)] |
Returns elements where corresponding logical value is TRUE |
| Character vector | v[c(“c”, “a”)] |
Returns elements with matching names (only for named vectors) |
| Nothing | v[] |
Returns the original vector |
| 0 (Zero) | v[0] |
Returns a zero-length vector |
: operator to generate vectors of indices for subsetting.seq() function provides a generalization of : for generating arithmetic progressions.For operations that require vectors to be of the same length R recycles (reuses) the shorter one:
list() function in R.str() - one of the most useful functions in R.[] to subset lists.[[ and $ operators.list[index]
list[[index]]
list$name