使元素NA取决于谓词函数 [英] Make elements NA depending on a predicate function
本文介绍了使元素NA取决于谓词函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何根据谓词轻松地将列表或向量的元素更改为NAs
?
How can I easily change elements of a list or vectors to NAs
depending on a predicate ?
我需要在单个调用中完成此操作,以便在dplyr::mutate
调用等中顺利集成...
I need it to be done in a single call for smooth integration in dplyr::mutate
calls etc...
预期输出:
make_na(1:10,`>`,5)
# [1] 1 2 3 4 5 NA NA NA NA NA
my_list <- list(1,"a",NULL,character(0))
make_na(my_list, is.null)
# [[1]]
# [1] 1
#
# [[2]]
# [1] "a"
#
# [[3]]
# [1] NA
#
# [[4]]
# character(0)
注意:
我回答了我的问题,因为我已经找到了一种解决方案,但我很高兴获得替代解决方案.另外,也许此功能已经存在于base R中或打包在一个显着的库中
查看全文