我在R
中有以下数据框 ID Name1 Name2 Name3 Name4 Name5
1 abc NA der def NA
2 NA NA NA NA NA
3 abc def rtg der ret
4 rty NA NA NA NA
我希望根据以下条件标记行:如果所有列中都存在所有值,那么它应该被标记为Yes
,如果所有值都是NA,那么它应该标记{ {1}}如果存在任何一个值,则应标记NO
我想要的数据框是
Partial
我怎样才能在R?
中完成答案 0 :(得分:1)
df$flag <- ifelse(rowSums(is.na(df)) == dim(df)[2] - 1, "NO", ifelse(rowSums(!is.na(df)) == dim(df)[2], "YES", "Partial"))
ID Name1 Name2 Name3 Name4 Name5 flag
1 1 abc <NA> der def <NA> Partial
2 2 <NA> <NA> <NA> <NA> <NA> NO
3 3 abc def rtg der ret YES
4 4 rty <NA> <NA> <NA> <NA> Partial
答案 1 :(得分:0)
您可以通过计算rowWise na值并随后链接2个if_else语句来轻松完成此操作:
library(tidyverse)
df %>%
mutate(NA_counter = rowSums(is.na(df))) %>%
mutate(Flag = if_else(NA_counter == 5, "No",
if_else(NA_counter > 0, "Partial", "Yes")))
答案 2 :(得分:0)
或者:
d <- read.table(text = "ID Name1 Name2 Name3 Name4 Name5
1 abc NA der def NA
2 NA NA NA NA NA
3 abc def rtg der ret
4 rty NA NA NA NA",
header = TRUE, stringsAsFactors = FALSE)
d$Flag <- apply(d[ , grepl("Name", colnames(d))], 1,
function(x) ifelse(all(is.na(x)), "No",
ifelse(any(is.na(x)), "Yes", "Partial") ))
d
答案 3 :(得分:0)
明确的非优雅方式......使用dplyr并假设列是字符
df2 <- df %>%
mutate(flag= ifelse(is.na(Name1) | is.na(Name2) | is.na(Name3) |
is.na(Name4) | is.na(Name5), "Partial", "Yes" )) %>%
mutate(flag= ifelse(is.na(Name1) & is.na(Name2) & is.na(Name3) &
is.na(Name4) & is.na(Name5), "No", flag))
答案 4 :(得分:0)
使用tidyverse
dplyr::case_when
解决方案
df %>%
mutate(
NA_count = apply(., 1, function(x) sum(is.na(x))),
Flag = case_when(
NA_count == 0 ~ "Yes",
NA_count < ncol(df) - 1 ~ "Partial",
TRUE ~ "No")) %>%
select(-NA_count)
# ID Name1 Name2 Name3 Name4 Name5 Flag
#1 1 abc <NA> der def <NA> Partial
#2 2 <NA> <NA> <NA> <NA> <NA> No
#3 3 abc def rtg der ret Yes
#4 4 rty <NA> <NA> <NA> <NA> Partial