在不创造新价值的情况下取平均值
Taking the average without creating new values
在下面的示例中,我将如何找到中间选项或“平均”?我不想通过取所有列的平均值来创建新值,并且取中位数在这种情况下也不起作用。我需要能够弄清楚蓝色的 (col_5) 是“中间”。有小费吗?谢谢!
col_1 <- c(0,32,34,36,37,41,43,44,47,48,50)
col_2 <- c(0,3,4,5,6,7,9,14,16,18,20)
col_3 <- c(0,22,23,25,28,31,32,35,38,39,41)
col_4 <- c(0,1,2,3,5,6,8,9,11,13,15)
col_5 <- c(0,2,5,9,11,15,25,33,36,37,38)
df1 <- data.frame(col_1, col_2, col_3, col_4, col_5)
plot(df1$col_1, type ="l")
lines(df1$col_2)
lines(df1$col_3)
lines(df1$col_4)
lines(df1$col_5, col='blue')
你需要调整我返回“中间”结果的方式,但基本上根据你的问题我认为你的问题是:
For all columns in a table, find the average, then determine which of those is the 'middle' or median
因此,为了实现这一点,我建议迭代各列以使用 sum(x) / length(x)
本质上的老式方法计算平均值:
avgs <- sapply(df1, function(i){
sum(i) / nrow(df1)
})
> avgs
col_1 col_2 col_3 col_4 col_5
37.45454545 9.27272727 28.54545455 6.63636364 19.18181818
# Just giving you a visual here
> sort(avgs)
col_4 col_2 col_5 col_3 col_1
6.63636364 9.27272727 19.18181818 28.54545455 37.45454545
所以现在我们只想知道哪个值是我们的中间值或median
> avgs[which(avgs == median(avgs))]
col_5
19.1818182
# OR if you just need the name:
> names(which(avgs == median(avgs)))
[1] "col_5"
在下面的示例中,我将如何找到中间选项或“平均”?我不想通过取所有列的平均值来创建新值,并且取中位数在这种情况下也不起作用。我需要能够弄清楚蓝色的 (col_5) 是“中间”。有小费吗?谢谢!
col_1 <- c(0,32,34,36,37,41,43,44,47,48,50)
col_2 <- c(0,3,4,5,6,7,9,14,16,18,20)
col_3 <- c(0,22,23,25,28,31,32,35,38,39,41)
col_4 <- c(0,1,2,3,5,6,8,9,11,13,15)
col_5 <- c(0,2,5,9,11,15,25,33,36,37,38)
df1 <- data.frame(col_1, col_2, col_3, col_4, col_5)
plot(df1$col_1, type ="l")
lines(df1$col_2)
lines(df1$col_3)
lines(df1$col_4)
lines(df1$col_5, col='blue')
你需要调整我返回“中间”结果的方式,但基本上根据你的问题我认为你的问题是:
For all columns in a table, find the average, then determine which of those is the 'middle' or median
因此,为了实现这一点,我建议迭代各列以使用 sum(x) / length(x)
本质上的老式方法计算平均值:
avgs <- sapply(df1, function(i){
sum(i) / nrow(df1)
})
> avgs
col_1 col_2 col_3 col_4 col_5
37.45454545 9.27272727 28.54545455 6.63636364 19.18181818
# Just giving you a visual here
> sort(avgs)
col_4 col_2 col_5 col_3 col_1
6.63636364 9.27272727 19.18181818 28.54545455 37.45454545
所以现在我们只想知道哪个值是我们的中间值或median
> avgs[which(avgs == median(avgs))]
col_5
19.1818182
# OR if you just need the name:
> names(which(avgs == median(avgs)))
[1] "col_5"