Reputation: 37
In my data set i have column 'Game' it's represented the games played , So i wanna calculate average number of game by year .
this is my data set .
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 ))
i tried many times but all of the code i wrote it was wrong
playerNBA[median(playerNBA$G),]
Upvotes: 0
Views: 114
Reputation: 21
dplyr is going to be your best friend here. The code below will group your rows by year and then using summarise, you can calculate the mean on your newly formed groups.
library(dplyr)
playerNBA %>%
group_by(yearID) %>%
summarise(avgGames = mean(Game))
Upvotes: 2