首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在r中折叠同一作者的每4个连续文本行

在r中折叠同一作者的每4个连续文本行
EN

Stack Overflow用户
提问于 2019-12-14 19:13:33
回答 1查看 43关注 0票数 2

我想将作者的每四篇文章组合在一个广泛的数据框架中,如果剩下的文章少于四篇,则合并这些帖子(例如,一个作者有11篇文章,我最终得到4篇文章中的2篇和3篇文章中的1篇)。

下面是我的数据框的一个示例:

代码语言:javascript
运行
复制
name  text
bee   _ so we know that right           
bee   said so           
alma  hello,            
alma  Good to hear back from you.           
bee   I've currently written an application         
alma  I'm happy about it            
bee   It was not the last.          
alma  Will this ever stop.          
alma  Yet another line.         
alma  so            

我想把它改成:

代码语言:javascript
运行
复制
name  text
bee   _ so we know that right said so I've currently written an application It was not the last.
alma  hello, Good to hear back from you. I'm happy about it Will this ever stop
alma  Yet another line. so

以下是初始数据帧:

代码语言:javascript
运行
复制
df = structure(list(name = c("bee", "bee", "alma", "alma", "bee", "alma", "bee", "alma", "alma", "alma"), text = c( "_ so we know that right", "said so", "hello,", "Good to hear back from you.", "I've currently written an application", "I'm happy about it", "It was not the last.", "Will this ever stop.", "Yet another line.", "so")), .Names = c("name", "text"), row.names = c(NA, -10L), class = "data.frame")
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-12-14 19:17:38

利用dplyr的一种选择可能是:

代码语言:javascript
运行
复制
df %>%
 group_by(name) %>%
 mutate(ID = ceiling(row_number()/4)) %>%
 group_by(name, ID) %>%
 summarise_all(paste, collapse = " ")

  name     ID text                                                                         
  <chr> <dbl> <chr>                                                                        
1 alma      1 hello, Good to hear back from you. I'm happy about it Will this ever stop.   
2 alma      2 Yet another line. so                                                         
3 bee       1 _ so we know that right said so I've currently written an application It was…
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59334585

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档