在我的数据集中,我有一列“game”,它代表了玩过的游戏,所以我想要计算每年的平均游戏数量。
这是我的数据集。
playerNBA <- data.frame(
playerID =c("abercda01" , "addybo01", "allisar01" ,
"allisdo01" , "ansonca01" , "armstbo01")
,yearID =c(1871 , 1871 , 1872 ,
1873 , 1873 , 1874 )
,stint =c(1 , 1 , 1,
1 , 1 , 1)
,teamID =c("TRO" , "RC1" , "CL1" ,
"WS3" , "RC1" , "FW1")
,lgID = c(NA , NA , NA , NA , NA , NA)
,Game = c( 1 , 25 , 29 , 27 , 25 , 12 ))我试了很多次,但我写的所有代码都是错误的
playerNBA[median(playerNBA$G),]发布于 2020-04-07 22:58:24
dplyr将会是你最好的朋友。下面的代码将按年份对行进行分组,然后使用汇总,您可以计算新形成的组的平均值。
library(dplyr)
playerNBA %>%
group_by(yearID) %>%
summarise(avgGames = mean(Game))https://stackoverflow.com/questions/61082789
复制相似问题