R-dplyr包使用的小总结

2022-07-26  本文已影响0人  睿睿爱汪汪也爱喵喵

1、如果调用了两个包,都有同样的函数,比如raster和dplyr都有select,就用dplyr::select

2、可以找出管道通道操作+contains、starts_with、ends_with

edu<-edu %>% select(contains("edu18"))

3、利用管道操作删样本

people<-sam[-which(sam$SAMPLEID%in%c("N130")),]

4、mutate操作(替换、新增变量超方便)

edu<-mutate(edu,EDU_UPDATE=ifelse(EDU_UPDATE==8,NA,EDU_UPDATE))

5、merge函数

edu<-merge(edu, place, by='SAMPLEID',all.x=TRUE)

6、!!、quo、unquo、!!!

详见vignette(“dplyr-programming”)

###两件事:(1)We need to quote the input ourselves;

(2)you use `!!` to say that you want to unquote an input so that it's evaluated, not quoted.

my_summarise <- function(df, group_var) {

  df %>%group_by(!! group_var) %>% #!!你想要取消输入的引号,以便对其进行评估

       summarise(a = mean(a))}

my_summarise(df, quo(g1))

###`!!!` takes a list of elements and splices them into to the current call.

args <- list(na.rm = TRUE, trim = 0.25)

quo(mean(x, !!! args))

args <- list(quo(x), na.rm = TRUE, trim = 0.25)

quo(mean(!!! args))

7、quo、unquo、!!!group分组+分组统计频数和百分比

fangyuan<-interviewer%>%group_by(编码)%>%summarise(n=n(),percentage=sum(反馈内容新)/sum(反馈内容总))

上一篇下一篇

猜你喜欢

热点阅读