【发布时间】:2020-09-05 20:11:15
【问题描述】:
令人惊讶的是,我没有找到这个简单问题的答案。我需要一种管道友好的方式来计算向量中非零元素的数量。
没有管道:
v <- c(1.1,2.2,0,0)
length(which(v != 0))
当我尝试使用管道执行此操作时出现错误
v %>% which(. != 0) %>% length
Error in which(., . != 0) : argument to 'which' is not logical
dplyr 解决方案也会有所帮助
【问题讨论】:
-
相关的,更一般的帖子:Using the %>% pipe, and dot (.) notation