代码是这样的。解释如下。
代码。
df = data.frame(primaryName = c("Aaron Lim", "Aaron Woodley"), tconst = c("tt2317744", "tt3228088"), primaryTitle = c("My friend Ron", "Spark: Some Title"), knownForTitles = c("tt0268228,tt0891369,tt2317744,tt3709694", "tt0326065,tt1650535,tt4426464,tt3228088"))
df$tconst = as.character(df$tconst)
Names = df %>%
mutate(V2 = strsplit(as.character(knownForTitles), ",")) %>%
tidyr::unnest(V2) %>%
select(-knownForTitles) %>%
as.data.frame(.)
Movies = df[,2:3]
Modi = left_join(Names, Movies, by = c("V2" = "tconst"))
Modi$primaryTitle.y = as.character(Modi$primaryTitle.y)
Modi[is.na(Modi$primaryTitle.y), "primaryTitle.y"] = "Test"
Modi %>%
group_by(tconst) %>%
summarise(primNew = stringr::str_c(primaryTitle.y, collapse = ", ")) %>%
inner_join(df, .)
输出。
primaryName tconst primaryTitle knownForTitles
1 Aaron Lim tt2317744 My friend Ron tt0268228,tt0891369,tt2317744,tt3709694
2 Aaron Woodley tt3228088 Spark: Some Title tt0326065,tt1650535,tt4426464,tt3228088
primNew
1 Test, Test, My friend Ron, Test
2 Test, Test, Test, Spark: Some Title
解释。
让我们定义一些玩具数据。
df = data.frame(primaryName = c("Aaron Lim", "Aaron Woodley"),
tconst = c("tt2317744", "tt3228088"),
primaryTitle = c("My friend", "Spark"),
knownForTitles = c("tt0268228,tt0891369,tt2317744,tt3709694", "tt0326065,tt1650535,tt4426464,tt3228088"))
df$tconst = as.character(df$tconst)
然后你可以用tidyr的unnest函数把所有的列字符串拆分成行,像这样
Names = df %>%
mutate(V2 = strsplit(as.character(knownForTitles), ",")) %>%
tidyr::unnest(V2) %>%
select(-knownForTitles) %>%
as.data.frame(.)
结果
> Names
primaryName tconst primaryTitle V2
1 Aaron Lim tt2317744 My friend Ron tt0268228
2 Aaron Lim tt2317744 My friend Ron tt0891369
3 Aaron Lim tt2317744 My friend Ron tt2317744
4 Aaron Lim tt2317744 My friend Ron tt3709694
5 Aaron Woodley tt3228088 Spark: Some Title tt0326065
6 Aaron Woodley tt3228088 Spark: Some Title tt1650535
7 Aaron Woodley tt3228088 Spark: Some Title tt4426464
8 Aaron Woodley tt3228088 Spark: Some Title tt3228088
然后你得到所有tconstants 的电影名称
Movies = df[,2:3]
Modi = left_join(Names, Movies, by = c("V2" = "tconst"))
结果
primaryName tconst primaryTitle.x V2 primaryTitle.y
1 Aaron Lim tt2317744 My friend Ron tt0268228 <NA>
2 Aaron Lim tt2317744 My friend Ron tt0891369 <NA>
3 Aaron Lim tt2317744 My friend Ron tt2317744 My friend Ron
4 Aaron Lim tt2317744 My friend Ron tt3709694 <NA>
5 Aaron Woodley tt3228088 Spark: Some Title tt0326065 <NA>
6 Aaron Woodley tt3228088 Spark: Some Title tt1650535 <NA>
7 Aaron Woodley tt3228088 Spark: Some Title tt4426464 <NA>
8 Aaron Woodley tt3228088 Spark: Some Title tt3228088 Spark: Some Title
由于这是玩具数据,有 NA 值会造成一些麻烦,所以我们这样做
Modi$primaryTitle.y = as.character(Modi$primaryTitle.y)
Modi[is.na(Modi$primaryTitle.y), "primaryTitle.y"] = "Test"
来应对。
最后,你修改匹配的电影并用
将它们折叠成一行
Modi %>%
group_by(tconst) %>%
summarise(primNew = stringr::str_c(primaryTitle.y, collapse = ", ")) %>%
inner_join(df, .)
结果
primaryName tconst primaryTitle knownForTitles
1 Aaron Lim tt2317744 My friend Ron tt0268228,tt0891369,tt2317744,tt3709694
2 Aaron Woodley tt3228088 Spark: Some Title tt0326065,tt1650535,tt4426464,tt3228088
primNew
1 Test, Test, My friend Ron, Test
2 Test, Test, Test, Spark: Some Title