本文介绍了R中多列的均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用R中列的平均值,并且遇到了问题.假设我有:
I am trying take the mean of a list of columns in R and am running into a issue. Let's say I have:
A B C D
1 2 3 4
5 6 7 8
9 10 11 12
我想做的是取列c(A,C)的均值并将其另存为值(E)以及列c(B,D)的均值并将其另存为a F表示不同的值.这可能吗?
What I am trying to do is take the mean of columns c(A,C) and save it as a value say (E) as well as the mean of columns c(B,D) and have it save as a different value say F. Is that possible?
E F
2 3
6 7
10 11
推荐答案
签出dplyr:
library(dplyr)
df <- df %>% mutate(E=(A+C)/2, F=(B+D)/2)
df
A B C D E F
1 1 2 3 4 2 3
2 5 6 7 8 6 7
3 9 10 11 12 10 11
这篇关于R中多列的均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!