我环顾了整个站点,但找不到此特定问题。我正在尝试准备数据集以进行进一步分析,但似乎无法修复某些问题。
我有一个球员及其目前所在俱乐部的列表:
PlayerID PlayerName CurrentClub
1 Roland Alberg ADO Den Haag
2 John Goossens Feyenoord
3 Michael de Leeuw Feyenoord
4 Kenny van der Weg NAC Breda
5 Alex Schalk NAC Breda
我想去的地方是:
NewID CurrentClub Player1 Player2
1 ADO Den Haag Roland Alberg NA
2 Feyenoord John Goossens Michael de Leeuw
3 NAC Breda Kenny van der Weg Alex Schalk
我已经尝试过使用melt,group_by和transpose的各种方法,但是我从来没有得到这个结果。
有人知道该怎么做吗?
答案 0 :(得分:3)
row_number和group_by的组合应该可以解决问题。这是我的解决方案:
df <- tibble(PlayerID = c(1,2,3,4,5),
PlayerName = c("Alberg", "Goossens","Leeuw","van der Weg","Schalk"),
CurrentClub = c("ADO Den Haag", "Feyenoord", "Feyenoord", "NAC Breda", "NAC Breda"))
new_df <- df %>% group_by(CurrentClub) %>% select(-PlayerID) %>%
mutate(player_number = paste0("Player ",row_number())) %>%
spread(player_number, PlayerName)
new_df
答案 1 :(得分:0)
df[,-1] %>%
group_by(CurrentClub) %>%
mutate(Player=seq(1:n())) %>%
spread(Player, PlayerName, sep="")