我正在与 mtcars
合作数据集。我正在尝试创建一个新数据集,其中仅包含非整数列。为此,我尝试了几件事,主要是尝试使用 lapply
像这样:
> newdata <- lapply(mtcars, Negate(is.integer))
但这并没有返回我需要的东西。在练习中,有人告诉我们 which() 可以帮助我们做到这一点,所以我问 - 如何?
谢谢
最佳答案
您可以使用Filter
来选择非整数的列。
Filter(Negate(is.integer), mtcars)
#Or explicitly mentioning non-integers
#Filter(function(x) !is.integer(x), mtcars)
在dplyr
中,我们可以使用select_if
library(dplyr)
mtcars %>% select_if(Negate(is.integer))
#mtcars %>% select_if(~!is.integer(.))
关于r - 使用which()从数据集中过滤掉列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60884961/