R语言随手记-数据处理

dplyr包

  • mutate(),将一列向量加入到已有的数据框,向量长度和数据框列数要一样
  • select(),选择数据框的某几列
  • filter(),对数据框的行进行筛选
df1 <- data.frame(
      name = c("Alice", "Alice", "Bob", "Bob", "Carol", "Carol"),
      type = c("english", "math", "english", "math", "english", "math")
)
## mutate()
df2<-mutate(df1, newcol=c(60, 85, 70, 75, 85, 90))

## select()
select(df2, name, newcol)
select(df2, !newcol)

## filter()
filter(newcol>75) #比如筛选newcol大于75的行
filter(type=="english",newcol>75) #只看英语成绩大于75
filter(dat, branch %in% dat$branch[grep("Foreground", dat$branch)])

https://bookdown.org/wangminjie/R4DS/dplyr.html

grep

target <- row.names(s1dat)[grep("fore", s1dat$mark)]

"=="

d$ccc[d$node==nodeid(s1tree,target)]<-"red"
posted @ 2021-03-17 18:12  Ryann'sBio  阅读(81)  评论(0编辑  收藏  举报