简体   繁体   English

统计dplyr中每一行非NA数值的个数

[英]Count the number of non-NA numeric values of each row in dplyr

I create a dataframe df.我创建了一个 dataframe df。

df <- data.frame (id = 1:10, 
    var1 = 10:19,
    var2 = sample(c(1:2,NA), 10, replace=T),
    var3 = sample(c(3:5, NA), 10, replace=T))

What I need is a new column var4, which count the number of non-NA values of each row (excluding the id column).我需要的是一个新列 var4,它计算每行的非 NA 值的数量(不包括 id 列)。 So for example, if a row is like var1=19, var2=1, var3=NA, then var4=2.因此,例如,如果一行类似于 var1=19、var2=1、var3=NA,则 var4=2。 I could not find a good way to do this in dplyr. something like:我在 dplyr 中找不到执行此操作的好方法。类似于:

df %in% mutate(var4= ... )

I appreciate if anyone can help me with that.如果有人可以帮助我,我将不胜感激。

Use select + is.na + rowSums , select(., -id) returns the original data frame ( . ) with id excluded, and then count number of non-NA values with rowSums(!is.na(...)) : 使用select + is.na + rowSumsselect(., -id) id select(., -id)返回包含id的原始数据帧( . ),然后使用rowSums(!is.na(...))计算非NA值的rowSums(!is.na(...))

df %>% mutate(var4 = rowSums(!is.na(select(., -id))))

#   id var1 var2 var3 var4
#1   1   10   NA    4    2
#2   2   11    1   NA    2
#3   3   12    2    5    3
#4   4   13    2   NA    2
#5   5   14    1   NA    2
#6   6   15    1   NA    2
#7   7   16    1    5    3
#8   8   17   NA    4    2
#9   9   18   NA    4    2
#10 10   19   NA   NA    1

I know the OP asked for a dplyr solution, but base R is straightforward here:我知道 OP 要求dplyr解决方案,但 base R 在这里很简单:

df$var4 <- rowSums(.is,na(df[:2:4]))

rowSums calculates the number of values that are not NA ( .is.na ) in columns 2 - 4. rowSums计算第 2 - 4 列中非 NA ( .is.na ) 的值的数量。

Note, this is summing the logical vector generated by is.na , which is distinct from:请注意,这是对is.na生成的逻辑向量求和,它不同于:

rowSums(df[,2:4], na.rm = TRUE)

Which drops the NA s and then sums the remaining values.它会丢弃NA ,然后对剩余值求和。

Another solution using only base-r另一种仅使用 base-r 的解决方案

data.frame(df, var4 = apply(df[,-1], 1, function(x) sum(!is.na(x))))
   id var1 var2 var3 var4
1   1   10    1    5    3
2   2   11    2    5    3
3   3   12    2    5    3
4   4   13   NA    3    2
5   5   14   NA    5    2
6   6   15    1    5    3
7   7   16   NA    3    2
8   8   17   NA    4    2
9   9   18   NA    3    2
10 10   19    1    4    3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM