【发布时间】:2021-07-10 20:14:07
【问题描述】:
我在 R 工作,遇到了一个有趣的问题。 我想转换下一个数据框:
DF = data.frame(ID = c(1, 2, 3),
Person1 = c("Devin Davey", "Rui Butt", "Keon Dotson"),
Sign = "artist",
Person2 = c("Eli Greer", "Alvin Simons", "Leona Ford"),
Sex = c("female", "male", "female"),
Score = c(10, 20, 30))
ID Person1 Sign Person2 Sex Score
1 1 Devin Davey artist Eli Greer female 10
2 2 Rui Butt artist Alvin Simons male 20
3 3 Keon Dotson artist Leona Ford female 30
格式如下:
ID Name Sign Score
1 1 Devin Davey artist 10
2 1 Eli Greer female 10
3 2 Rui Butt artist 20
4 2 Alvin Simons male 20
5 3 Keon Dotson artist 30
6 3 Leona Ford female 30
也就是说,有一个特殊的将四列连接成两个新列的方法。
我有一个想法如下:
PART1 <- DF %>%
select(ID, Person1, Person2, Score) %>%
gather(key, Name, -c(ID, Score), na.rm = TRUE) %>%
select(-key) %>%
arrange(ID) %>%
mutate(temp_id = 1:n())
PART2 <- DF %>%
select(ID, Sign, Sex) %>%
gather(key, Sign, -ID, na.rm = TRUE) %>%
select(-key) %>%
arrange(ID) %>%
mutate(temp_id = 1:n())
PART1 %>%
left_join(PART2, by = c("ID" = "ID", "temp_id" = "temp_id")) %>%
select(-temp_id) %>%
relocate(Score, .after = Sign)
但是在我看来这样的解决方案不是很漂亮,我认为这个问题可以用更好的方式解决。
因此,我将感谢您使用tidyverse 解决此问题的想法。
【问题讨论】:
标签: r dplyr tidyverse tidyr data-manipulation