I have a very large dataset containing historical soccer results. Here is a portion of it:
Season home visitor FT
1954 Aston Villa SHW 0-0
1956 Aston Villa SHW 5-0
1957 Aston Villa SHW 2-0
1960 Aston Villa SHW 4-1
1987 Aston Villa HUL 5-0
1987 Aston Villa HUD 1-1
1987 Aston Villa BLB 1-1
1933 Preston North End NOT 4-0
1958 Preston North End NOT 3-5
1960 Preston North End NOT 0-1
1962 Preston North End SWA 6-3
1976 Walsall SHW 5-1
1977 Walsall SHW 1-1
2002 Walsall Sheffield United 0-1
2002 Walsall Gillingham 1-0
For each home team (factor), I wish to return the unique levels of another factor (Season) that occur for that factor. In the above example, this would return:
Aston Villa - 1954, 1956, 1957, 1960, 1987
Preston North End - 1933, 1958, 1960, 1962
Walsall - 1976, 1977, 2002
I thought about trying to do this in dplyr as an exercise. However, I'm not getting this right.
I tried this:
library(dplyr)
demodf%>%
group_by(home)%>%
summarize(levels(Season))
#Error: expecting a single value
Just out of interest, I did the following to see if I could see the first year returned for each factor/home team:
demodf%>%
group_by(home)%>%
summarize(levels(Season)[1])
this gave me this:
# home levels(Season)[1]
#1 Aston Villa 1933
#2 Preston North End 1933
#3 Walsall 1933
which is not right - it has just returned the first level of the Season factor in the whole dataframe (1933), rather than the first year/level of the Season factor for each team respectively - I thought the group.by
would have helped get at this.
I appreciate any help with this.
The below should enable you to reproduce the above table:
demodf<-structure(list(Season = structure(c(2L, 3L, 4L, 6L, 10L, 10L,
10L, 1L, 5L, 6L, 7L, 8L, 9L, 11L, 11L), .Label = c("1933", "1954",
"1956", "1957", "1958", "1960", "1962", "1976", "1977", "1987",
"2002"), class = "factor"), home = structure(c(1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L), .Label = c("Aston Villa",
"Preston North End", "Walsall"), class = "factor"), visitor = structure(c(7L,
7L, 7L, 7L, 4L, 3L, 1L, 5L, 5L, 5L, 8L, 7L, 7L, 6L, 2L), .Label = c("BLB",
"Gillingham", "HUD", "HUL", "NOT", "Sheffield United", "SHW",
"SWA"), class = "factor"), FT = structure(c(1L, 9L, 5L, 8L, 9L,
4L, 4L, 7L, 6L, 2L, 11L, 10L, 4L, 2L, 3L), .Label = c("0-0",
"0-1", "1-0", "1-1", "2-0", "3-5", "4-0", "4-1", "5-0", "5-1",
"6-3"), class = "factor")), .Names = c("Season", "home", "visitor",
"FT"), row.names = c(NA, -15L), class = "data.frame")
In this case, you can just use by
:
with(demodf, by(Season, home, unique))
# home: Aston Villa
# [1] 1954 1956 1957 1960 1987
# Levels: 1933 1954 1956 1957 1958 1960 1962 1976 1977 1987 2002
# ------------------------------------------------------------
# home: Preston North End
# [1] 1933 1958 1960 1962
# Levels: 1933 1954 1956 1957 1958 1960 1962 1976 1977 1987 2002
# ------------------------------------------------------------
# home: Walsall
# [1] 1976 1977 2002
# Levels: 1933 1954 1956 1957 1958 1960 1962 1976 1977 1987 2002
The "data.table" package can also handle list
s as columns in a data.table
, like this:
library(data.table)
DT <- as.data.table(demodf)
DT[, list(Season = list(unique(Season))), by = home]
# home Season
# 1: Aston Villa 1954,1956,1957,1960,1987
# 2: Preston North End 1933,1958,1960,1962
# 3: Walsall 1976,1977,2002
Note the structure of the result:
str(.Last.value)
# Classes ‘data.table’ and 'data.frame': 3 obs. of 2 variables:
# $ home : Factor w/ 3 levels "Aston Villa",..: 1 2 3
# $ Season:List of 3
# ..$ : Factor w/ 11 levels "1933","1954",..: 2 3 4 6 10
# ..$ : Factor w/ 11 levels "1933","1954",..: 1 5 6 7
# ..$ : Factor w/ 11 levels "1933","1954",..: 8 9 11
# - attr(*, ".internal.selfref")=<externalptr>
Having Season
as a factor complicates matters slightly, however
demodf %>% group_by(home) %>% do(data.frame(Seasons = unique(.$Season)))
will work.
Note it is simpler to use unique
instead of levels
I used paste to mimic the output you wanted:
demodf%>%
group_by(home)%>%
summarise( summary = paste(unique(Season),collapse=","))
which gives
home summary
1 Aston Villa 1954,1956,1957,1960,1987
2 Preston North End 1933,1958,1960,1962
3 Walsall 1976,1977,2002
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.