本文介绍了计算组均值,同时排除每个个案的单个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含70个案例的数据集(一项研究的参与者).是否有一个函数可以计算这70个案例的平均值,以使每个个案都不包含在分析中.看起来像:
I have a dataset with 70 cases (participants in a study). Is there a function that can calculate the mean of these 70 cases such that each individual case is not included in the analysis. This would look like:
"mean for case x = (value(1) + ... value(n) - value(x))/n"
任何信息都会有所帮助.
Any information will help.
推荐答案
您可以按照建议执行操作,并从总计中删除每种情况:
You could just do what you've suggested and remove each case from the total:
x <- c(1:10)
(sum(x) - x) / (length(x) - 1)
#[1] 6.000000 5.888889 5.777778 5.666667 5.555556 5.444444 5.333333 5.222222 5.111111 5.000000
mean(2:10)
#[1] 6
mean(1:9)
#[1] 5
更新以尝试解决评论中的后续问题:
Updated to try to address followup question in comments:
set.seed(123)
df <- data.frame(group = rep(letters[1:3], each = 3),
value = rnorm(9), stringsAsFactors = F)
df
#group value
#1 a -0.56047565
#2 a -0.23017749
#3 a 1.55870831
#4 b 0.07050839
#5 b 0.12928774
#6 b 1.71506499
#7 c 0.46091621
#8 c -1.26506123
#9 c -0.68685285
df$loo_mean <- unlist(tapply(df$value, df$group,
function(x) (sum(x) - x) / (length(x) - 1)))
df
#group value loo_mean
#1 a -0.56047565 0.66426541
#2 a -0.23017749 0.49911633
#3 a 1.55870831 -0.39532657
#4 b 0.07050839 0.92217636
#5 b 0.12928774 0.89278669
#6 b 1.71506499 0.09989806
#7 c 0.46091621 -0.97595704
#8 c -1.26506123 -0.11296832
#9 c -0.68685285 -0.40207251
mean(df$value[2:3])
#[1] 0.6642654
mean(df$value[c(7,9)])
#[1] -0.1129683
这篇关于计算组均值,同时排除每个个案的单个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!