简体   繁体   English

在带有管道函数的向量中应用 is.na() 函数

[英]Applying is.na() function in a vector with pipe function

I was experimenting with the pipe function from dplyr , and running below code without success -我正在尝试dplyrpipe功能,并且在代码下运行但没有成功 -

library(dplyr)
12 %>% ifelse(is.na(.), FALSE, TRUE)
### Error in ifelse(., is.na(.), FALSE, TRUE) : unused argument (TRUE)

Any pointer why I am getting this error?任何指针为什么我收到这个错误? What is the correct approach if I want to use pipe ?如果我想使用pipe ,正确的方法是什么?

Another option is to unnest the functions further.另一种选择是进一步取消嵌套函数。

12 %>% is.na %>% ifelse(FALSE, TRUE)
# TRUE

We can use is.na directly in the pipeline rather than nesting it:我们可以直接在管道中使用is.na而不是嵌套它:

12 %>% is.na %>% ifelse(FALSE, TRUE)

or或者

12 %>% is.na %>% `!`

or或者

library(magrittr)
12 %>% is.na %>% not

or或者

12 %>% (is.na %>% Negate)

There are several possibilities:有几种可能:

Using brackets : As @tmfmnk pointed out, one should use {} to get that behavior in pipe :使用方括号:正如@tmfmnk 所指出的,应该使用{}来获取pipe中的行为:

 library(dplyr)
 vec <- c(12,13, NA)
 vec %>% {ifelse(is.na(.), FALSE,TRUE)}
 # TRUE  TRUE FALSE

Unnesting : Alternatively, as @Ands pointed out, one can unnest the functions:取消嵌套:或者,正如@Ands 指出的那样,可以取消嵌套函数:

12 %>% is.na %>% ifelse(FALSE, TRUE)
library(dplyr)
12 %>% ifelse(is.na(.), FALSE)
#FALSE

this works for me这对我有用

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

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