R包tidyverse学习
2020-02-21 本文已影响0人
M78_a
教程来自: 慕课
微生物高通量数据分析入门
学习目标:
filter,arrange,mutate,select函数,R中的管道符%>%的运用
开始吧
使用内置的数据集diamonds作为练习
library(tidyverse)
#filter筛选
diamonds%>%
filter(color == "D")
diamonds%>%
filter(color == "D",cut =="Ideal")
#arrange排列,排序
diamonds%>%
arrange(price)#升序排列
diamonds%>%
arrange(desc(price))#降序排列
#filter和arrange联合
diamonds%>%
filter(color == "D")%>%
arrange(desc(price))
or
arrange(filter(diamonds,color == "D"),desc(price))
#mutate是改变或者增加一个变量
diamonds%>%
mutate(price*7)#直接在末尾增加一列
diamonds%>%
mutate(price = price*7) #增加一列并且赋值给原来price列
diamonds%>%
mutate(price_RMB = price*7)#增加一列名为price_RMB
diamonds%>%
filter(color == "D")%>%
mutate(price_RMB = price*7)%>%
arrange(price_RMB)
#select按列选择
diamonds %>%
select(color,cut)
#summarise总结
diamonds%>%
summarise(sum(price))
sum(diamonds$price)
mean(diamonds$price)
#好像这两种方法效果一样。