Basics of R (Review)¶
Data Frames¶
A Data Frame is a special kind of List where all elements are vectros fo the same length.
[10]:
data.frame(a=1:5, b=6:10, c=rnorm(5))
| a | b | c |
|---|---|---|
| <int> | <int> | <dbl> |
| 1 | 6 | -0.2701866 |
| 2 | 7 | 0.4624501 |
| 3 | 8 | 0.1019228 |
| 4 | 9 | -1.5094554 |
| 5 | 10 | 0.4304919 |
Basic Data Frame manipulation¶
[11]:
df <- data.frame(a=1:5, b=6:10, c=rnorm(5))
[12]:
df
| a | b | c |
|---|---|---|
| <int> | <int> | <dbl> |
| 1 | 6 | 0.5207780 |
| 2 | 7 | -1.5697044 |
| 3 | 8 | 0.3914048 |
| 4 | 9 | -0.5415737 |
| 5 | 10 | -0.9471250 |
Get rows¶
[16]:
df[3,]
| a | b | c | |
|---|---|---|---|
| <int> | <int> | <dbl> | |
| 3 | 3 | 8 | 0.3914048 |
[17]:
df[c(1,3), ]
| a | b | c | |
|---|---|---|---|
| <int> | <int> | <dbl> | |
| 1 | 1 | 6 | 0.5207780 |
| 3 | 3 | 8 | 0.3914048 |
Get sub-frame¶
[19]:
df[c(1,3)]
| a | c |
|---|---|
| <int> | <dbl> |
| 1 | 0.5207780 |
| 2 | -1.5697044 |
| 3 | 0.3914048 |
| 4 | -0.5415737 |
| 5 | -0.9471250 |
[20]:
df[c(1,3), 2:3]
| b | c | |
|---|---|---|
| <int> | <dbl> | |
| 1 | 6 | 0.5207780 |
| 3 | 8 | 0.3914048 |
[ ]: