R:计算data.frame数据row均值

R计算data.frame行row平均方法有2种 :rowMeans()和apply()

创建data.frame,计算平均分Avg_score

df1 = data.frame( Name = c(‘Pyyy’,’Pyu’, ‘Mic’,’Maie’,’Rvi’,’Xin’,’Jan’),
Mathematics1_score=c(88,57,50,73,30,97,89),
Mathematics2_score=c(45,78,44,89,66,49,72),
Science_score=c(56,52,45,88,33,90,47))

计算方法如下

方法1.rowMeans()

df1$Avg_score = rowMeans(df1[,c(2,3,4)])

方法2.apply()

df1$Avg_score = apply(df1[,-1], 1, mean)

R:计算data.frame数据row均值

原创文章,作者:bio,如若转载,请注明出处:https://www.ouq.net/r%e8%ae%a1%e7%ae%97data-frame%e6%95%b0%e6%8d%aerow%e5%9d%87%e5%80%bc.html

发表评论

登录后才能评论