我有两个数据框,其中数据通过“ID”标签连接,如下所示:
test_frame1<- data.frame(
ID = c("A","B","C"),
ElementType1 = c(1,6,1),
ElementType2= c(4,4,5),
ElementType3 = c('',6,1),
Notes = c("Something random","","Something else random")
)
test_frame2<-data.frame(
ID = c("A","A","A","A","A","A","B","B","B","B","B","B","B","B","B","C","C","C","C","C","C","C","C","C"),
Syllable = c(1,1,2,2,3,3,1,1,1,2,2,2,3,3,3,1,1,1,2,2,2,3,3,3),
ElementID = c(1,2,1,2,1,2,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3)
)
我想向 test_frame2 添加一个新列来表示元素类型,如 test_frame1 中的中间列所述。我希望这些元素类型对每个不同的音节重复,如下所示:
desired_frame<-data.frame(
ID = c("A","A","A","A","A","A","B","B","B","B","B","B","B","B","B","C","C","C","C","C","C","C","C","C"),
Syllable = c(1,1,2,2,3,3,1,1,1,2,2,2,3,3,3,1,1,1,2,2,2,3,3,3),
ElementID = c(1,2,1,2,1,2,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3),
ElementType= c(1,4,1,4,1,4,6,4,6,6,4,6,6,4,6,1,5,1,1,5,1,1,5,1)
)
我首先转置了 test_frame1 ,使元素类型位于同一列中,ID 为列名称。然后我尝试将 test_frame2 分成按 ID 和音节分组的列表。此时,我希望将元素类型列中的值从 test_frame1 合并、cbind 或以其他方式粘贴到该 ID 的相应列表项到 test_frame2 中。看起来“for 循环”也可能对此有所帮助。不过,我正在努力寻找正确的函数来根据列表元素标题的一部分合并列表和多列。有什么简单的方法可以解决这个问题吗?
您可以将
test_frame1
转换为长格式,然后从 left_join
执行 test_frame2
到结果上:
library(tidyverse)
test_frame2 %>%
left_join(test_frame1 %>%
mutate(across(starts_with("Element"), as.numeric)) %>%
pivot_longer( starts_with("Element"), names_pattern = "(\\d+)",
names_to = "ElementID", values_to = "Type") %>%
mutate(ElementID = as.numeric(ElementID)), by = c("ID", "ElementID")) %>%
select(-Notes) %>%
rename(ElementType = Type)
#> ID Syllable ElementID ElementType
#> 1 A 1 1 1
#> 2 A 1 2 4
#> 3 A 2 1 1
#> 4 A 2 2 4
#> 5 A 3 1 1
#> 6 A 3 2 4
#> 7 B 1 1 6
#> 8 B 1 2 4
#> 9 B 1 3 6
#> 10 B 2 1 6
#> 11 B 2 2 4
#> 12 B 2 3 6
#> 13 B 3 1 6
#> 14 B 3 2 4
#> 15 B 3 3 6
#> 16 C 1 1 1
#> 17 C 1 2 5
#> 18 C 1 3 1
#> 19 C 2 1 1
#> 20 C 2 2 5
#> 21 C 2 3 1
#> 22 C 3 1 1
#> 23 C 3 2 5
#> 24 C 3 3 1
创建于 2023-08-01,使用 reprex v2.0.2