-
Notifications
You must be signed in to change notification settings - Fork 3
/
Subsetting.Rmd
44 lines (41 loc) · 1.05 KB
/
Subsetting.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
Subsetting
========================================================
<big>**Some hints:**</big>
*Functions to look up (Technically,* `women` *is a default dataset in R)*
```
?seq
?"["
?women
```
*Code examples*
Note that this output has '#'es before it for ease of copying and pasting.
```{r women-examples}
#Generate sequence from 1 to 10 in steps of 2
seq(from=1, to=10, by=2)
#Show the dataframe women that comes with R
women
#Show the first row of women
women[1, ]
#Show the first column of women
women[, 1]
#Show the column of women titled "height"
women[,"height"]
#Get the 2nd, 3rd and 7th rows
women[c(2,3,7),]
#Show women without the 2nd, 3rd and 7th row.
women[-c(2, 3, 7), ]
#How many rows and columns does the data have?
nrow(women)
ncol(women)
#Get the last row in the first column
women[nrow(women),1]
```
<big>**Your mission**</big>
Take this dataframe:
```{r sample-data}
dat <- data.frame(a=c(1,2,4,5,2), b=c(3,5,7,34,3), c=c(7,2,3,6,7))
dat
```
And get every odd-numbered row of the third column:
```{r subset-solution, echo=FALSE}
dat[seq(1,nrow(dat),2), 3]