从作为向量提供的多个变量中逐行计算 NA
Count NAs rowwise from multiple variables provided as vector
我有一个数据:
test_df <- data.frame(x1 = c("a", "b", "c", NA, NA),
x2 = sample(1:5),
x3 = c(T, NA, F, T, NA),
x4 = c(NA, NA, 1, 2, 3),
stringsAsFactors = F)
colset1 <- c("x1", "x2", "x3")
colset2 <- c("x2", "x3", "x4")
包含变量名称的数据框和向量。
如果向量 (colset
) 中提供的列的任何行包含任何 NA
,如何检查(最好以 dplyr
方式)?
colset1
的预期答案是 TRUE FALSE TRUE FALSE FALSE
,colset2
的预期答案是 FALSE FALSE TRUE TRUE FALSE
.
备选问题是:如何计算该列中的 NA
?
colset1
的预期答案是 0 1 0 1 2
,colset2
的预期答案是 1 2 0 0 1
我正在尝试改变 ...ifelse(length(sum(is.na(vars(colset1)))) == 0)
但仍然缺少一些东西,它没有用,我迷失在自己的代码中:)
谢谢!
不需要dplyr
。您可以简单地使用 rowSums
,即
!rowSums(is.na(test_df[colset1])) > 0
#[1] TRUE FALSE TRUE FALSE FALSE
!rowSums(is.na(test_df[colset2])) > 0
#[1] FALSE FALSE TRUE TRUE FALSE
要得到实际的NA数,可以去掉逻辑> 0
和取反符(!
),即
rowSums(is.na(test_df[colset1]))
#[1] 0 1 0 1 2
rowSums(is.na(test_df[colset2]))
#[1] 1 2 0 0 1
一个dplyr
解决方案可以是:
test_df %>%
mutate(colset1 = Reduce(`|`, across(colset1, ~ is.na(.))),
colset2 = Reduce(`|`, across(colset2, ~ is.na(.))))
获取计数:
test_df %>%
mutate(colset1 = rowSums(across(colset1, ~ is.na(.))),
colset2 = rowSums(across(colset2, ~ is.na(.))))
x1 x2 x3 x4 colset1 colset2
1 a 3 TRUE NA 0 1
2 b 4 NA NA 1 2
3 c 1 FALSE 1 0 0
4 <NA> 5 TRUE 2 1 0
5 <NA> 2 NA 3 2 1
test_df
x1 x2 x3 x4
1 a 4 TRUE NA
2 b 3 NA NA
3 c 2 FALSE 1
4 NA 5 TRUE 2
5 NA 1 NA 3
is_na(test_df)
x1 x2 x3 x4
[1,] FALSE FALSE FALSE TRUE
[2,] FALSE FALSE TRUE TRUE
[3,] FALSE FALSE FALSE FALSE
[4,] TRUE FALSE FALSE FALSE
[5,] TRUE FALSE TRUE FALSE
使用na.tools包
我有一个数据:
test_df <- data.frame(x1 = c("a", "b", "c", NA, NA),
x2 = sample(1:5),
x3 = c(T, NA, F, T, NA),
x4 = c(NA, NA, 1, 2, 3),
stringsAsFactors = F)
colset1 <- c("x1", "x2", "x3")
colset2 <- c("x2", "x3", "x4")
包含变量名称的数据框和向量。
如果向量 (colset
) 中提供的列的任何行包含任何 NA
,如何检查(最好以 dplyr
方式)?
colset1
的预期答案是 TRUE FALSE TRUE FALSE FALSE
,colset2
的预期答案是 FALSE FALSE TRUE TRUE FALSE
.
备选问题是:如何计算该列中的 NA
?
colset1
的预期答案是 0 1 0 1 2
,colset2
的预期答案是 1 2 0 0 1
我正在尝试改变 ...ifelse(length(sum(is.na(vars(colset1)))) == 0)
但仍然缺少一些东西,它没有用,我迷失在自己的代码中:)
谢谢!
不需要dplyr
。您可以简单地使用 rowSums
,即
!rowSums(is.na(test_df[colset1])) > 0
#[1] TRUE FALSE TRUE FALSE FALSE
!rowSums(is.na(test_df[colset2])) > 0
#[1] FALSE FALSE TRUE TRUE FALSE
要得到实际的NA数,可以去掉逻辑> 0
和取反符(!
),即
rowSums(is.na(test_df[colset1]))
#[1] 0 1 0 1 2
rowSums(is.na(test_df[colset2]))
#[1] 1 2 0 0 1
一个dplyr
解决方案可以是:
test_df %>%
mutate(colset1 = Reduce(`|`, across(colset1, ~ is.na(.))),
colset2 = Reduce(`|`, across(colset2, ~ is.na(.))))
获取计数:
test_df %>%
mutate(colset1 = rowSums(across(colset1, ~ is.na(.))),
colset2 = rowSums(across(colset2, ~ is.na(.))))
x1 x2 x3 x4 colset1 colset2
1 a 3 TRUE NA 0 1
2 b 4 NA NA 1 2
3 c 1 FALSE 1 0 0
4 <NA> 5 TRUE 2 1 0
5 <NA> 2 NA 3 2 1
test_df
x1 x2 x3 x4
1 a 4 TRUE NA
2 b 3 NA NA
3 c 2 FALSE 1
4 NA 5 TRUE 2
5 NA 1 NA 3
is_na(test_df)
x1 x2 x3 x4
[1,] FALSE FALSE FALSE TRUE
[2,] FALSE FALSE TRUE TRUE
[3,] FALSE FALSE FALSE FALSE
[4,] TRUE FALSE FALSE FALSE
[5,] TRUE FALSE TRUE FALSE
使用na.tools包