r - Dynamic counting of occurrences -


r newb. small rep of data.

teamhome <- c("lal", "hou", "sas", "lal") teamaway <- c("ind", "sas", "lal", "hou") df <- data.frame(cbind(teamhome, teamaway)) df     teamhome teamaway      lal      ind      hou      sas      sas      lal      lal      hou 

imagine these first 4 games of season thousands of games. home team , visitor team want compute cumulative number of games played @ home, on road , total. 3 new columns both home team , visiting team. (in case calculating new variables home team):

    teamhome teamaway hometeamgamesplayedathome hometeamgamesplayedroad hometeamtotalgames 1      lal      ind                         1                       0                  1 2      hou      sas                         1                       0                  1 3      sas      lal                         1                       1                  2 4      lal      hou                         2                       1                  3 

to compute first column (hometeamgamesplayedathome) managed with:

df$hometeamgamesplayedathome <- ave(df$teamhome==df$teamhome, df$teamhome, fun=cumsum) 

but feels on complicated , can't calculate other columns approach.

i thought of using formula table count number of occurrences:

 table(df$teamhome) 

but computes totals , want result @ given point in time. thanks!

library(dplyr) df <- df %>% group_by(teamhome) %>%    mutate(homegames = seq_along(teamhome)) lst <- list() for(i in 1:nrow(df)) lst[[i]] <- sum(df$teamaway[1:i] == df$teamhome[i]) df$hometeamgamesplayedroad <- unlist(lst) df %>% mutate(hometeamtotalgames = homegames+hometeamgamesplayedroad)   teamhome teamaway homegames hometeamgamesplayedroad homegames 1      lal      ind         1                       0         1 2      hou      sas         1                       0         1 3      sas      lal         1                       1         2 4      lal      hou         2                       1         3 

homegames created seq_along iterated row. hometeamgamesplayedroad created loop checking values in teamaway , including current game. final row sum of other 2 created.


Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -