繁体   English   中英

对于一个因子的所有级别,请使用dplyr从同一数据帧返回另一个因子的所有级别。 [R

[英]For all levels of a factor, return all levels of another factor from same dataframe - using dplyr ? r

我有一个非常大的数据集,其中包含历史足球成绩。 这是其中的一部分:

  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

对于每个主队(因素),我希望返回该因素发生的另一个因素(季节)的唯一水平。 在上面的示例中,它将返回:

Aston Villa - 1954, 1956, 1957, 1960, 1987
Preston North End - 1933, 1958, 1960, 1962
Walsall - 1976, 1977, 2002

我考虑过要尝试在dplyr中执行此操作。 但是,我做错了。

我尝试了这个:

library(dplyr)
demodf%>%
group_by(home)%>%
summarize(levels(Season))
#Error: expecting a single value

出于兴趣,我做了以下事情,看看是否可以看到每个因素/主队的第一年回报:

demodf%>%
group_by(home)%>%
summarize(levels(Season)[1])

这给了我这个:

#               home levels(Season)[1]
#1       Aston Villa              1933
#2 Preston North End              1933
#3           Walsall              1933

这是不对的-它刚刚返回了整个数据帧(1933)中第一季度的季节因子,而不是分别返回每个团队的第一年/季节因子的水平-我认为group.by会帮助获得在这。

我对此表示感谢。

下面应该使您能够复制上表:

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")

在这种情况下,您可以使用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

“ data.table”包还可以将list s作为data.table列来data.table ,如下所示:

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

注意结果的结构:

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> 

但是,将Season作为因素会使事情变得复杂些

demodf %>% group_by(home) %>% do(data.frame(Seasons = unique(.$Season)))

将工作。

请注意,使用unique而不是levels更简单

我使用粘贴来模仿您想要的输出:

demodf%>%
  group_by(home)%>%
  summarise( summary =  paste(unique(Season),collapse=","))

这使

               home                  summary
1       Aston Villa 1954,1956,1957,1960,1987
2 Preston North End      1933,1958,1960,1962
3           Walsall           1976,1977,2002

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM