首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >突变粘贴

突变粘贴
EN

Stack Overflow用户
提问于 2022-04-10 16:24:36
回答 3查看 40关注 0票数 1

我有一个日期,议程,发言者,党和文本的数据(发言者说)。我想重塑数据,以便我有日期,议程,党和合并文本。一直试图与变异,总结,汇总和pmap无效。

有什么想法吗?我非常喜欢避免for循环,因为它们需要三个层,而且我的数据相当大。

到目前为止,我所掌握的内容是:

代码语言:javascript
复制
combining <- df %>% group_by(date, agenda, party) %>% mutate(combined_text = paste(text, sep = "_"))

示例df (抱歉,它是这种笨重的格式):

代码语言:javascript
复制
           date                                                   agenda party
4908 1988-12-13 Pensioners (Health) [Oral Answers To Questions > Health]   Lab
4909 1988-12-13 Pensioners (Health) [Oral Answers To Questions > Health]   Lab
4910 1988-12-13 Pensioners (Health) [Oral Answers To Questions > Health]   Con
4911 1988-12-13 Pensioners (Health) [Oral Answers To Questions > Health]   Lab
4912 1988-12-13 Pensioners (Health) [Oral Answers To Questions > Health]   Con
4913 1988-12-13 Pensioners (Health) [Oral Answers To Questions > Health]   Con
                   speaker
4908      Alistair Darling
4909           Peter Hardy
4910         Edwina Currie
4911      Alistair Darling
4912         Edwina Currie
4913 Michael McNair-Wilson
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        text
4908                                                                                                                                                                                                                                                                                             To ask the Secretary of State for Health if he plans to add to the advice given by his Under-Secretary of State, the hon. Member for Derbyshire, South (Mrs. Currie) at Reading, to pensioners on keeping healthy during the winter months.
4909                                                                                                                                                                                                                                To ask the Secretary of State for Health if he has received any representations from pensioners or pensioners' organisations about the advice given by the Under-Secretary of State, the hon. Member for Derbyshire, South (Mrs. Currie) at Reading on the subject of keeping healthy during the winter.
4910 On pensioners' health day in Reading the Government drew attention to simple advice on self-help which can make a difference to winter mortality. For the second year running we have a " Keep Warm, Keep Well" campaign, involving the five Government Departments and voluntary organisations, which is proving to be very effective. We have received around 400 letters on the topic. The telephone helpline, run by Help the Aged, is now receiving over 700 calls per week. We are very pleased with the success of the campaign.
4911                                                                                    On reflection, does the hon. Lady consider that her remarks were ill-judged and stupid? For how much longer will she be allowed to act as court jester, deflecting attention from the fact that for many pensioners this Christmas the choice will be between heating their houses and eating? Is she aware that the best Christmas present that she could give most pensioners, and, indeed, many Conservative Members, would be a month's silence.
4912                                                                                                                                                             The hon. Gentleman seems to have forgotten that the worst winter in recent years for excess winter mortality and hypothermia was 1979. If the Opposition had their way, there would be no such campaign. There was no campaign in the 1970s when winter mortality was much higher than now. The advice is plain common sense and the Opposition would do better to back it.
4913                                                                                                                                                                                                                                                                                                                  I congratulate my hon. Friend on her advice for the elderly. Will she confirm that about 20 per cent. of body heat can be lost through the top of the head and that if one wore a hat, the heat loss would be reduced.
EN

回答 3

Stack Overflow用户

发布于 2022-04-10 16:36:30

下面的应该做你想做的事。注意summarize而不是mutate的使用,以及paste内部的“崩溃”

代码语言:javascript
复制
combining <- df %>% group_by(date, agenda, party) %>% summarize(combined_text = paste(text, collapse = "_"))
票数 2
EN

Stack Overflow用户

发布于 2022-04-10 16:39:55

您这样做是对的,在paste0()中使用paste0(),也就是当希望行减少使用summarise()时,因为mutate()添加了一个列,但没有减少记录。

样本数据:

代码语言:javascript
复制
df <- data.frame(
  Date = c('1988-12-13','1988-12-13','1988-12-13','1988-12-13','1988-12-14','1988-12-14')
  ,agenda = c('Pensions','Pensions','Pensions','Pensions','1988-12-14','1988-12-14')
  ,party = c('ABC','ABC','ABC','ABC','DEF','DEF')
  ,text = c('Text1','text2','text3','text4','text5','text6')
           )

现在:

代码语言:javascript
复制
df%>%
  group_by(Date,agenda,party)%>%
  summarise(CombinedText = paste0(text,collapse = "_"))

产出:

代码语言:javascript
复制
 Date       agenda     party CombinedText           
  <chr>      <chr>      <chr> <chr>                  
1 1988-12-13 Pensions   ABC   Text1_text2_text3_text4
2 1988-12-14 1988-12-14 DEF   text5_text6  
票数 2
EN

Stack Overflow用户

发布于 2022-04-10 16:40:51

使用tidyverse,我们可以使用str_c

代码语言:javascript
复制
library(dplyr)
library(stringr)
combined <- df %>%
     group_by(date, agenda, party) %>%
     summarise(CombinedText = str_c(text, collapse = "_"))
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71818478

复制
相关文章

相似问题

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