简体   繁体   English

用NA分组计算每个值

[英]Counting each values by groups with NAs

I'm using this code for counting rows by fours. 我正在使用此代码对四分之一的行进行计数。 But it doesn't work when my df includes NA. 但是,当我的df包含NA时,它不起作用。 How can I overcome this? 我该如何克服?

count <- sapply(split.default(df, 0:(length(df)-1) %/% 4), rowSums)

Normally rowSums has na.rm=TRUE but when I tried it here I'm getting this: 通常rowSums具有na.rm = TRUE,但是当我在这里尝试时,我得到了这个:

Error in is.data.frame(x) : argument "x" is missing, with no default

I tried different versions of lapply, sapply or apply but no one worked. 我尝试了lapply,sapply或apply的不同版本,但没有人起作用。 I'm just a starter so it will be very simple but I couldn't success. 我只是一个初学者,所以这将非常简单,但我无法成功。

Thanks in advance. 提前致谢。

EDIT: Small example: 编辑:小例子:

id <- 1:12
b <- c(0,0,1,0,0,1,1,0,0,0,1,1)
df <-data.frame(b,b,b,b,b,b,b,b,b,b,b)
df$yeni <- sapply(split.default(df, 0:(length(df)-1) %/% 4), rowSums)
#   b b.1 b.2 b.3 b.4 b.5 b.6 b.7 b.8 b.9 b.10 yeni.0 yeni.1 yeni.2
#1  0   0   0   0   0   0   0   0   0   0    0      0      0      0
#2  0   0   0   0   0   0   0   0   0   0    0      0      0      0
#3  1   1   1   1   1   1   1   1   1   1    1      4      4      3
#4  0   0   0   0   0   0   0   0   0   0    0      0      0      0
#5  0   0   0   0   0   0   0   0   0   0    0      0      0      0
#6  1   1   1   1   1   1   1   1   1   1    1      4      4      3
#7  1   1   1   1   1   1   1   1   1   1    1      4      4      3
#8  0   0   0   0   0   0   0   0   0   0    0      0      0      0
#9  0   0   0   0   0   0   0   0   0   0    0      0      0      0
#10 0   0   0   0   0   0   0   0   0   0    0      0      0      0
#11 1   1   1   1   1   1   1   1   1   1    1      4      4      3
#12 1   1   1   1   1   1   1   1   1   1    1      4      4      3

My code doesn't work when my dataset is like this one 当我的数据集像这样时,我的代码不起作用

d <- c(0,NA,1,0,0,1,1,0,0,0,1,1)
df <-data.frame(b,b,b,b,b,b,b,b,b,b,b,d,d)

I would make df an array and then use rowSums : 我将df制成一个数组,然后使用rowSums

b <- c(0,0,1,0,0,1,1,0,0,0,1,1)
d <- c(0,NA,1,0,0,1,1,0,0,0,1,1)
df <-data.frame(b,b,b,b,b,b,b,b,b,b,b,d,d)
#convert to matrix
a <- as.matrix(df)
#fill with NA values and convert to array
i <- nrow(a)
j <- ceiling(ncol(a) / 4)
length(a) <- i * j * 4
dim(a) <- c(i, j, 4)
#rearrange dimensions of array
a <- aperm(a, c(1,3,2))
#calculate the sums
rowSums(a, na.rm = TRUE, dims = 2)
#      [,1] [,2] [,3] [,4]
# [1,]    0    0    0    0
# [2,]    0    0    0    0
# [3,]    4    4    4    1
# [4,]    0    0    0    0
# [5,]    0    0    0    0
# [6,]    4    4    4    1
# [7,]    4    4    4    1
# [8,]    0    0    0    0
# [9,]    0    0    0    0
#[10,]    0    0    0    0
#[11,]    4    4    4    1
#[12,]    4    4    4    1

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

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