【发布时间】:2018-07-31 17:25:22
【问题描述】:
我有一个包含以下列的数据框:
> colnames(my.dataframe)
[1] "id" "firstName" "lastName"
[4] "position" "jerseyNumber" "currentTeamId"
[7] "currentTeamAbbreviation" "currentRosterStatus" "height"
[10] "weight" "birthDate" "age"
[13] "birthCity" "birthCountry" "rookie"
[16] "handednessShoots" "college" "twitter"
[19] "currentInjuryDescription" "currentInjuryPlayingProbability" "teamId"
[22] "teamAbbreviation" "fg2PtAtt" "fg3PtAtt"
[25] "fg2PtMade" "fg3PtMade" "ftMade"
[28] "fg2PtPct" "fg3PtPct" "ftPct"
[31] "ast" "tov" "offReb"
[34] "foulsDrawn" "blkAgainst" "plusMinus"
[37] "minSeconds"
这是我的代码不起作用:
my.dataframe %>%
dplyr::group_by(id) %>%
dplyr::summarise_at(vars(firstName:currentInjuryPlayingProbability), funs(min), na.rm = TRUE) %>%
dplyr::summarise_at(vars(fg2PtAtt:minSeconds), funs(sum), na.rm = TRUE) %>%
vars(), funs(min), na.rm = TRUE) %>%
dplyr::summarise(teamId = paste(teamId), teamAbbreviation = paste(teamAbbreviation))
首先我按 id 分组(这不是我的数据框中的唯一列,尽管它被称为 id)。对于直到 currentInjuryPlayingProbability 的接下来的 19 列,这些列在 grouped_by ID 时总是相同的,因此我使用min 函数来汇总/获取值。
接下来,我想用平均值总结从fg2PtAtt 到末尾的所有列(这些列都是数字/整数)。
最后,对于列 teamId 和 teamAbbreviation(在 grouped_by id 时不一样),我想将它们粘贴到一个单独的字符串中,每个字符串都有摘要。
我的方法不起作用,因为我认为我不能调用 summarise_at,然后调用另一个 summarise_at,然后再调用 summarise。在调用第二个 summarise_at 时,试图汇总的列已被第一个 summarise_at 删除
对此的任何帮助表示赞赏!我将很快更新我的数据帧的一个子集,以便测试代码。
编辑:
dput(my.dataframe)
structure(list(id = c(10138L, 9466L, 9360L, 9360L), firstName = c("Alex",
"Quincy", "Luke", "Luke"), lastName = c("Abrines", "Acy", "Babbitt",
"Babbitt"), currentInjuryPlayingProbability = c(NA_character_,
NA_character_, NA_character_, NA_character_), teamId = c(96L,
84L, 91L, 92L), teamAbbreviation = c("OKL", "BRO", "ATL", "MIA"
), fg2PtAtt = c(70L, 73L, 57L, 2L), fg3PtAtt = c(221L, 292L,
111L, 45L), minSeconds = c(67637L, 81555L, 34210L, 8676L)), row.names = c(NA,
-4L), class = c("tbl_df", "tbl", "data.frame"))
my.dataframe
id firstName lastName currentInjuryPlayingProbability teamId teamAbbreviation fg2PtAtt fg3PtAtt minSeconds
<int> <chr> <chr> <chr> <int> <chr> <int> <int> <int>
1 10138 Alex Abrines <NA> 96 OKL 70 221 67637
2 9466 Quincy Acy <NA> 84 BRO 73 292 81555
3 9360 Luke Babbitt <NA> 91 ATL 57 111 34210
4 9360 Luke Babbitt <NA> 92 MIA 2 45 8676
这是一个只有 9 列的简短示例,但有足够的数据来突出问题。生成的数据框应如下所示:
id firstName lastName currentInjuryPlayingProbability teamId teamAbbreviation fg2PtAtt fg3PtAtt minSeconds
<int> <chr> <chr> <chr> <chr> <chr> <int> <int> <int>
1 10138 Alex Abrines <NA> 96 OKL 70 221 67637
2 9466 Quincy Acy <NA> 84 BRO 73 292 81555
3 9360 Luke Babbitt <NA> 91, 92 ATL, MIA 57 156 42886
【问题讨论】:
-
在第一个 summarise_at 之后,您会得到一个汇总输出,第二个正在调用该汇总输出。我认为更好的选择是首先调用
mutate_at,然后在 group_by 中使用它,然后再进行 summarise_at。无论如何,一个可重复的小例子会有所帮助 -
刚刚发布了一个可重现的小例子。
-
是的,我注意到 summarise_at 发生了这种情况 - 只是不知道如何解决它
-
看起来像 mutate_at(),然后删除重复的列,可能会成功
-
我认为您可能想使用
mutate_at,因为summarize具有破坏性。来自?summarize:Each summary call removes one grouping level (since that group is now just a single row)和Note that with data frames, newly created summaries immediately overwrite existing variables