如何获得所选列的平均值(平均值) [英] How can I get the average (mean) of selected columns
问题描述
我想获得每行某些列的平均值.
I would like to get the average for certain columns for each row.
我有这个数据:
w=c(5,6,7,8)
x=c(1,2,3,4)
y=c(1,2,3)
length(y)=4
z=data.frame(w,x,y)
哪个返回:
w x y
1 5 1 1
2 6 2 2
3 7 3 3
4 8 4 NA
我想获得某些列的平均值,而不是所有列的平均值.我的问题是我的数据中有很多 NA.所以如果我想要 x 和 y 的平均值,这就是我想要的:
I would like to get the mean for certain columns, not all of them. My problem is that there are a lot of NAs in my data. So if I wanted the mean of x and y, this is what I would like to get back:
w x y mean
1 5 1 1 1
2 6 2 2 2
3 7 3 3 3
4 8 4 NA 4
我想我可以做类似 z$mean=(z$x+z$y)/2
的事情,但是 y 的最后一行是 NA,所以显然我不希望 NA 是计算出来的,我不应该除以二.我尝试了 cumsum
但是当该行中有一个 NA 时它会返回 NA.我想我正在寻找可以添加所选列的东西,忽略 NA,获取没有 NA 的所选列的数量并除以该数字.我试过 ??mean 和 ??average 并且完全被难住了.
I guess I could do something like z$mean=(z$x+z$y)/2
but the last row for y is NA so obviously I do not want the NA to be calculated and I should not be dividing by two. I tried cumsum
but that returns NAs when there is a single NA in that row. I guess I am looking for something that will add the selected columns, ignore the NAs, get the number of selected columns that do not have NAs and divide by that number. I tried ??mean and ??average and am completely stumped.
ETA:是否还有一种方法可以为特定列添加权重?
ETA: Is there also a way I can add a weight to a specific column?
推荐答案
以下是一些示例:
> z$mean <- rowMeans(subset(z, select = c(x, y)), na.rm = TRUE)
> z
w x y mean
1 5 1 1 1
2 6 2 2 2
3 7 3 3 3
4 8 4 NA 4
加权平均
> z$y <- rev(z$y)
> z
w x y mean
1 5 1 NA 1
2 6 2 3 2
3 7 3 2 3
4 8 4 1 4
>
> weight <- c(1, 2) # x * 1/3 + y * 2/3
> z$wmean <- apply(subset(z, select = c(x, y)), 1, function(d) weighted.mean(d, weight, na.rm = TRUE))
> z
w x y mean wmean
1 5 1 NA 1 1.000000
2 6 2 3 2 2.666667
3 7 3 2 3 2.333333
4 8 4 1 4 2.000000
这篇关于如何获得所选列的平均值(平均值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!