Use the built-in data set airquality.
- Extract the first 4 rows of the data frame and print them to the console.
1
2
3
| write.table(airquality, airquality.txt)
x <- read.table(airquality.txt, nrow = 4)
x
|
Ozone Solar.R Wind Temp Month Day
1 41 190 7.4 67 5 1
2 36 118 8.0 72 5 2
3 12 149 12.6 74 5 3
4 18 313 11.5 62 5 4
方法二
1
2
3
4
5
6
7
8
9
| head(airquality, n=4)
Ozone Solar.R Wind Temp Month Day
1 41 190 7.4 67 5 1
2 36 118 8.0 72 5 2
3 12 149 12.6 74 5 3
4 18 313 11.5 62 5 4
|
- Extract the last 3 rows of the data frame and print them to the console.
方法一
1
2
3
4
5
6
7
8
9
10
| x <- read.table(airquality.txt, skip = 151 , nrow = 3)
x
V1 V2 V3 V4 V5 V6 V7
1 151 14 191 14.3 75 9 28
2 152 18 131 8.0 76 9 29
3 153 20 223 11.5 68 9 30
|
方法二
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| tail(airquality, n=3)
Ozone Solar.R Wind Temp Month Day
151 14 191 14.3 75 9 28
152 18 131 8.0 76 9 29
153 20 223 11.5 68 9 30
3. What is the value of Wind in the 24th row?
x$Wind[24]
[1] 12
|
- What is the mean of Temp when Month is equal to 8?
方法一
1
2
3
4
5
| c<- data.table(airquality)
c[Month==8,.(y=mean(Temp))]
y
1: 83.96774
|
方法二
1
2
3
| mean(x$Temp[x$Month==8])
[1] 83.96774
|
- Extract the subset of rows of the data frame where Ozone values are above 30 and Temp values are above 80. What is the mean of Solar.R in this subset?
1
2
3
| mean(x$Solar.R[x$Ozone>30 &x$Temp>80], na.rm = T)
[1] 220.6512
|
- How many missing values are in the Solar.R column of this data frame?
1
2
3
| length(x$Solar.R[is.na(x$Solar.R)])
[1] 7
|