r-突变和粘贴



我有一个数据帧,其中包含日期、议程、发言人、聚会和文本(发言人说了(。我想重塑数据,以便我有日期、议程、聚会和合并文本。一直试图用变异、总结、聚合和pmap都无济于事。

知道吗?我真的更喜欢避免for循环,因为它们需要三层,而且我的数据相当大。

到目前为止,我所拥有的是:

combining <- df %>% group_by(date, agenda, party) %>% mutate(combined_text = paste(text, sep = "_"))

示例df(很抱歉它是这种笨拙的格式(:

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.

以下内容应该可以满足您的需要。注意使用summarize代替mutate;塌陷";paste:内部

combining <- df %>% group_by(date, agenda, party) %>% summarize(combined_text = paste(text, collapse = "_"))

如果做得对,在paste0()中使用collapse =,当行数预计会减少时,也可以使用summarise(),因为mutate()添加了一列,但没有减少记录。

样本数据:

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')
)

现在:

df%>%
group_by(Date,agenda,party)%>%
summarise(CombinedText = paste0(text,collapse = "_"))

输出:

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  

使用tidyverse,我们可以使用str_c

library(dplyr)
library(stringr)
combined <- df %>%
group_by(date, agenda, party) %>%
summarise(CombinedText = str_c(text, collapse = "_"))

最新更新