Reading and Writing Data 抓出指定變數或觀測值


Use the built-in data set airquality.

  1. Extract the first 4 rows of the data frame and print them to the console.
    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

方法二

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
  1. Extract the last 3 rows of the data frame and print them to the console.

方法一

 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

方法二

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
  1. What is the mean of Temp when Month is equal to 8?

方法一

c<- data.table(airquality)
c[Month==8,.(y=mean(Temp))]
 y

1: 83.96774

方法二

mean(x$Temp[x$Month==8])

[1] 83.96774
  1. 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?

    mean(x$Solar.R[x$Ozone>30 &x$Temp>80], na.rm = T)
    
    [1] 220.6512
  2. How many missing values are in the Solar.R column of this data frame?

    length(x$Solar.R[is.na(x$Solar.R)])
    
    [1] 7

Author: Hung-Lin, Chen
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint policy. If reproduced, please indicate source Hung-Lin, Chen !
  TOC