我有一个包含评论字段的数据数据框。在某些数据行中,有单行没有注释(注释字段中为 NA)。数据中的某些位置有不止一行可能包含也可能不包含注释。
数据类似于此结构(尽管有更多字段):
input <- data.frame(
stringsAsFactors = FALSE,
Location = c(1L, 1L, 1L, 2L, 2L, 3L, 4L),
Comment = c("This is a comment", NA, "This is another comment", "This is a comment", NA, "This is a comment", NA)
)
Run Code Online (Sandbox Code Playgroud)
Location Comment
1 This is a comment
1 NA
1 This is another comment
2 This is a comment
2 NA
3 This is a comment
4 NA
Run Code Online (Sandbox Code Playgroud)
我可以使用组连接这个并总结如下:
output <- input %>%
group_by(Location) %>%
summarise(Comment = paste(Comment, collapse = …Run Code Online (Sandbox Code Playgroud)