简体   繁体   English

特殊使用colSums(),仅当缺少1个或更少的时候na.rm = TRUE

[英]Special use of colSums(), na.rm = TRUE only if 1 or fewer are missing

I need to sum some columns in a data.frame with a rule that says, a column is to be summed to NA if more than one observation is missing NA if only 1 or less missing it is to be summed regardless. 我需要总结的一些列的data.frame与说的规则,一列是求和以NA如果超过一个观察缺少NA如果只有1或更低失踪了,无论是要进行求和。

Say I have some data like this, 说我有一些这样的数据,

dfn <- data.frame(
a  = c(3, 3, 0, 3),
b  = c(1, NA, 0, NA),
c  = c(0, 3, NA, 1))

dfn
  a  b  c
1 3  1  0
2 3 NA  3
3 0  0 NA
4 3 NA  1

and I apply my rule, and sum the columns with less then 2 missing NA . 然后我应用我的规则,并对缺失的NA少于2的列求和。 So I get something like this. 所以我得到这样的东西。

  a  b  c
1 3  1  0
2 3 NA  3
3 0  0 NA
4 3 NA  1
5 9 NA  4

I've played around with colSums(dfn, na.rm = FALSE) and colSums(dfn, na.rm = TRUE) . 我玩过colSums(dfn, na.rm = FALSE)colSums(dfn, na.rm = TRUE) In my real data there is more then three columns and also more then 4 rows. 在我的真实数据中,多于三列,多于4行。 I imagine I can count the missing some way and use that as a rule? 我想我可以以某种方式计算失踪人数并将其用作规则?

I don't think you can do this with colSums alone, but you can add to its result using ifelse : 我不认为您可以单独使用colSums来做到这colSums ,但是可以使用ifelse来添加其结果:

colSums(dfn,na.rm=TRUE) + ifelse(colSums(is.na(dfn)) > 1, NA, 0)
 a  b  c 
 9 NA  4 

Nothing wrong with @James' Answer, but here's a slightly cleaner way: @James'Answer没问题,但是这是一种更简洁的方法:

colSums(apply(dfn, 2, function(col) replace(col, match(NA, col), 0)))
# a  b  c 
# 9 NA  4 

match(NA, col) returns the index of the first NA in col, replace replaces it with 0 and returns the new column, and apply returns a matrix with all of the new columns. match(NA, col)返回match(NA, col)中第一个NA的索引, replace将其替换为0并返回新列, apply返回包含所有新列的matrix

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

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