简体   繁体   English

R dplyr删除“ <NA> 表格中的“列”

[英]R dplyr remove “<NA>” column from table

Given data 给定数据

d <- data.frame(g = sample(c(1:5,NA),100, replace = T)
                ,o = sample(c("yes","no",NA),100, replace = T))

Is there a one step approach to the following: 有没有一种一步方法:

s <- d %>% group_by(g, o) %>% 
  summarise(n = n()) %>%
  ungroup() %>%
  spread(o, n, fill=0) 
s %>% select(which(names(s)!="<NA>"))

If I do this: 如果我这样做:

s <- d %>% group_by(g, o) %>% 
  summarise(n = n()) %>%
  ungroup() %>%
  spread(o, n, fill=0) %>% select(-c("<NA>"))

I get the error: 我得到错误:

Error in -c("<NA>") : invalid argument to unary operator

names(s) gives 名称给出

"g"    "no"   "yes"  "<NA>"

As @Psidom says in the comment, the following works. 就像@Psidom在评论中所说,以下工作。 It matters if you use quotes vs back ticks. 无论您使用引号还是反引号,都非常重要。

s <- d %>% group_by(g, o) %>% 
summarise(n = n()) %>%
ungroup() %>%
spread(o, n, fill=0) %>% select(-`<NA>`)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM