R 计算每列的平均值并重新整形

R 计算每列的平均值并重新整形,r,dataframe,R,Dataframe,我有一个ID为229的学生的csv数据集,其中: StudID Score Weight 229 65 51 229 45 43 229 82 79 我要计算学生的平均分数和权重,我想得到如下结果: Measurements Mean Value #Measurem

我有一个ID为229的学生的csv数据集,其中:

StudID            Score            Weight
 229                65               51
 229                45               43
 229                82               79
我要计算学生的平均分数和权重,我想得到如下结果:

Measurements   Mean Value   #Measurements and mean value are new column names

Score          64         #score and weight which used to be column names are now under measurements
Weight         57.67
因此,我目前所做的工作如下:

stud_data <- read.csv("student_weight.csv")
stud_mean <- colMeans(stud_data[2:3])    #finding the mean of only score and weight
是否可以以我将获得的方式格式化输出:

Measurements        Mean Value          #Measurements and Mean value are new column names

   Score                64
   Weight               57.67

我不完全确定我是否理解你的意图。您是在问如何一次聚合多个列吗?或者这是关于如何重塑聚合数据的

关于前者,在R基你可以

aggregate(cbind(Score, Weight) ~ StudID, df, FUN = mean)
#  StudID Score   Weight
#1    229    64 57.66667
对于后者,可以使用堆栈来重塑形状

stack(aggregate(cbind(Score, Weight) ~ StudID, df, FUN = mean))
#     values    ind
#1 229.00000 StudID
#2  64.00000  Score
#3  57.66667 Weight
这里我假设您的实际数据包含多个StudID的数据,因此您可能希望按StudID聚合数据

样本数据
这基本上是一个从短到长的问题,最简单的方法是使用tidyr::gather:

您可以使用stack-after-colMeans

要分配列名,我们可以使用setNames


在这种情况下,基于StudID的聚合会是stackaggregatecbindScore,Weight,by=liststud\u data$StudID,df,FUN=mean吗?@Maxxx Just stackaggregatecbindScore,Weight~StudID,df,FUN=mean,请看我答案的第二部分,我在那里做的就是这个。根据您的示例数据,我假设StudID是data.frame的一列。如果在第二个答案中首先是测量值,然后是平均值,是否可能?@Maxxx这只是关于重新排列列。您可以执行setNamesstackcolMeansdf[2:3][2:1],Cmeasures,Mean_Value或setNamesstackcolMeansdf[2:3],cMean_Value,Measures[2:1]
stack(aggregate(cbind(Score, Weight) ~ StudID, df, FUN = mean))
#     values    ind
#1 229.00000 StudID
#2  64.00000  Score
#3  57.66667 Weight
df <- read.table(text =
    "StudID            Score            Weight
 229                65               51
 229                45               43
 229                82               79", header = T)
data.frame('Score'=64, 'Weight'=57.67) %>%
    tidyr::gather('Measurements', 'Value')

  Measurements Value
1        Score 64.00
2       Weight 57.67
stack(colMeans(df[2:3]))
#  values    ind
#1 64.00000  Score
#2 57.66667 Weight
setNames(stack(colMeans(df[2:3])), c("Mean_Value", "Measurements"))

#    Mean_Value  Measurements
#1     64.00000        Score
#2     57.66667        Weight