R 使用索引将多行合并为一行

R 使用索引将多行合并为一行,r,R,根据这一输入: structure(list(mid = c("text11", "text12", "text21", "text22", "text23"), term = c("test", "text", "section", "2", "sending" )), class = &quo

根据这一输入:

structure(list(mid = c("text11", "text12", "text21", "text22", 
"text23"), term = c("test", "text", "section", "2", "sending"
)), class = "data.frame", row.names = c(NA, -5L))
如何使用mid将其转换为单个熔体行。在这部分的中间,text1,text2。。。text12显示行数和术语在此行中存在的新编号。将它们与空间分隔合并

数据帧示例

data.frame(mid = c("text1", "text2"), term = c("test "text", "section 2 sending"
))
这应该行得通

library(dplyr)
library(stringr)
df <- structure(list(mid = c("text11", "text12", "text21", "text22", 
                       "text23"), term = c("test", "text", "section", "2", "sending"
                       )), class = "data.frame", row.names = c(NA, -5L))

df %>% 
  mutate(mid = str_extract(mid, "text\\d")) %>% 
  group_by(mid) %>% 
  summarise(term = paste(term, collapse=" "))
# # A tibble: 2 x 2
#   mid   term             
#   <chr> <chr>            
# 1 text1 test text        
# 2 text2 section 2 sending
库(dplyr)
图书馆(stringr)
df%
突变(mid=str_extract(mid,“text\\d”))%>%
组别(中期)%>%
总结(术语=粘贴(术语,折叠=”)
##tibble:2x2
#中期
#                
#1文本1测试文本
#2文本2第2节发送

编辑-发表评论

针对注释中的问题,以下函数适用于除最后一个数字以外的所有数字标识组的任何情况(即下面示例中的1和12)

df%
突变(mid=str_sub(mid,1,(nchar(mid)-1)))%>%
组别(中期)%>%
总结(术语=粘贴(术语,折叠=”)
##tibble:2x2
#中期
#                 
#1文本1测试文本
#2文本12第2节发送

谢谢。如果你有这个输入
结构(列表(mid=c(“text11”、“text12”、“text121”、“text122”、“text123”)、term=c(“test”、“text”、“section”、“2”、“sending”))、class=“data.frame”、row.names=c(NA,-5L))
@demia我刚刚编辑了答案来处理你在评论中提出的问题。
df <- structure(list(mid = c("text11", "text12", "text121", "text122",  "text123"), term = c("test", "text", "section", "2", "sending")), class = "data.frame", row.names = c(NA, -5L))

df %>% 
  mutate(mid = str_sub(mid, 1, (nchar(mid)-1))) %>% 
  group_by(mid) %>% 
  summarise(term = paste(term, collapse=" "))

# # A tibble: 2 x 2
#   mid    term             
#   <chr>  <chr>            
# 1 text1  test text        
# 2 text12 section 2 sending