我正在使用此命令删除所有值都为NA的列。
testing5 <- subset(testing4,
select = -c(kurtosis_picth_belt, skewness_roll_belt,
skewness_roll_belt.1, min_yaw_belt, amplitude_yaw_belt,
kurtosis_roll_arm, kurtosis_picth_arm, kurtosis_yaw_arm,
skewness_roll_arm, skewness_pitch_arm, kurtosis_picth_dumbbell,
skewness_roll_dumbbell, skewness_pitch_dumbbell, min_yaw_dumbbell,
kurtosis_roll_forearm, kurtosis_picth_forearm, skewness_roll_forearm,
skewness_pitch_forearm))
是否有更短的(程序化)方法?
谢谢和问候, 帕塔
答案 0 :(得分:3)
您可以删除包含所有NA
值的列,例如
d <- data.frame(x = c(NA, 3, NA), y = rep(NA, 3))
# x y
# 1 NA NA
# 2 3 NA
# 3 NA NA
d[!sapply(d, function(x) all(is.na(x)))]
# x
# 1 NA
# 2 3
# 3 NA
关于您的数据,这将是
testing4[!sapply(testing4, function(x) all(is.na(x)))]
答案 1 :(得分:2)
使用@Richards数据的另一种方式(更多矢量化)
d[!is.nan(colMeans(d, na.rm = TRUE))]
# x
# 1 NA
# 2 3
# 3 NA
答案 2 :(得分:0)
tidyverse方法如下所示(也使用@Rich Scriven数据):
d %>% select_if(~any(!is.na(.)))
# x
# 1 NA
# 2 3
# 3 NA