您的位置 首页 R

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)

热门文章

发表评论