计算一列中的非NA编号并输出数据帧 [英] Count non-NA numbers in a column and output a data frame
问题描述
例如,我有以下数据框(df):
For example, I have this data frame (df):
Color X1 X2 X3 X4
Red 1 1 0 2
Blue 0 NA 4 1
Red 3 4 3 1
Green 2 2 1 0
我想创建一个函数,该函数将X2中非NA的数量作为颜色的函数.我希望在名为newdf的新数据框中输出此函数.这就是我想要的输出:
I would like to create a function that counts up the number of non-NAs in X2 as a function of color. I would like the output of this function in a new data frame named newdf. This is what I would like for output:
Color X2
Red 2
Blue NA
Green 1
到目前为止,我有以下代码:
So far, I have this code:
Question <- function(Color){
Result <-
rowsum((df[c("X2")] > 0) + 0, df[["X2"]], na.rm = TRUE)
rowSums(Result)[[Color]]
}
Question("Red")
此函数提供的输出仅为Question("Red")= 2
,而我想在一个新的数据帧(newdf)中获取所有颜色的结果.有人能帮忙吗?谢谢!
The output this function gives is just Question("Red")= 2
and I would like to instead get my results of all the colors in a new data frame (newdf). Can anyone help with this? Thanks!
推荐答案
或者如果您想使用data.table:
Or if you wanted to use data.table:
library(data.table)
dt[,sum(!is.na(X2)),by=.(Color)]
Color V1
1: Red 2
2: Blue 0
3: Green 1
它也很容易在数据表中使用ifelse()
来获得蓝色(而不是0)的NA.请参阅:
Also its easy enough to use an ifelse()
in your data.table to get an NA for blue instead of 0. See:
dt[,ifelse(sum(!is.na(X2)==0),as.integer(NA),sum(!is.na(X2))),by=.(Color)]
Color V1
1: Red 2
2: Blue NA
3: Green 1
数据:
dt <- as.data.table(fread("Color X1 X2 X3 X4
Red 1 1 0 2
Blue 0 NA 4 1
Red 3 4 3 1
Green 2 2 1 0"))
这篇关于计算一列中的非NA编号并输出数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!