主要内容
Day6-学习R包.png
实际操作
- 打开R自带数据集
library(dplyr)
airquality=tbl_df(airquality) #airquality为R自带数据集
df <- tbl_df(airquality)
df
# A tibble: 153 x 6
Ozone Solar.R Wind Temp Month Day
<int> <int> <dbl> <int> <int> <int>
1 41 190 7.40 67 5 1
2 36 118 8.00 72 5 2
3 12 149 12.6 74 5 3
4 18 313 11.5 62 5 4
5 NA NA 14.3 56 5 5
6 28 NA 14.9 66 5 6
7 23 299 8.60 65 5 7
8 19 99 13.8 59 5 8
9 8 19 20.1 61 5 9
10 NA 194 8.60 69 5 10
# ... with 143 more rows
- muture变形
mutate(airquality,Temp_C = (Temp - 30) / 1.5)
# A tibble: 153 x 7
Ozone Solar.R Wind Temp Month Day Temp_C
<int> <int> <dbl> <int> <int> <int> <dbl>
1 41 190 7.4 67 5 1 24.7
2 36 118 8 72 5 2 28
3 12 149 12.6 74 5 3 29.3
4 18 313 11.5 62 5 4 21.3
5 NA NA 14.3 56 5 5 17.3
6 28 NA 14.9 66 5 6 24
7 23 299 8.6 65 5 7 23.3
8 19 99 13.8 59 5 8 19.3
9 8 19 20.1 61 5 9 20.7
10 NA 194 8.6 69 5 10 26
# ... with 143 more rows
- select选择
select(df,1)
# A tibble: 153 x 1
Ozone
<int>
1 41
2 36
3 12
4 18
5 NA
6 28
7 23
8 19
9 8
10 NA
# ... with 143 more rows
- filter筛选
filter(df,Temp < 60 & Wind > 15)
filter(df,Temp > 60)
- arrange排列
arrange(df,Wind,Temp_C)
arrange(df,desc(Wind))
- 管道操作
airquality %>%
+ group_by(Month) %>%
+ summarise(mean_temp=mean(Temp)) %>%
+ arrange(mean_temp)
网友评论