简体   繁体   English

在data.frame中缺少值的列之间求和

[英]summing across columns with missing values in a data.frame

i want to get the index of a column with the highest value. 我想获取具有最高值的列的索引。 However, I don't know how to handle missing values to make the correct calculation. 但是,我不知道如何处理缺失值以进行正确的计算。 NAs should be omitted (=ignored during summing up) and not converted to "0". NA应该省略(=在累加过程中被忽略),并且不转换为“ 0”。

x=rep(NA,3); y=c(NA,0,-1); z=c(0, rep(NA,2))
data=cbind(x,y,z)

     x  y  z
[1,] NA NA  0
[2,] NA  0 NA
[3,] NA -1 NA

I want to get the index of a column with the highest value. 我想获取具有最高值的列的索引。 In the example above it's [,3] . 在上面的示例中,它是[,3] However the functions 但是功能

   which.max(colSums(!is.na(data)))

or 要么

apply(data,2,sum, na.rm=T)

don't generate the expected output. 不要产生预期的输出。

Any help appreciated. 任何帮助表示赞赏。 Thx. 谢谢。

You can determine the column index of the column whose sum is greatest among the columns with non missing values in this way: 您可以通过以下方式确定总和最大的列索引:

dataAvailIdx <- which(apply(data,2,function(x) any(!is.na(x))))
dataAvailIdx[which.max(colSums(data[,dataAvailIdx],na.rm=TRUE))]

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

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