Search code examples
rdataframetextmerge

Merging rows in a character data frame


I have a collection of texts which are organised in a data frame in the following way:

Original Data frame

I would need such texts to be organised in the following way

Final Data Frame

I have been through a lot of previous questions here, but all merging suggested includes calculations, something which is not the case here. I have also consulted Tidytext package but did not seem to find a function to merge text in this way.

Any help is appreciated.

Edit

A pice of the actual data frame would be:

dput(df1)
structure(list(Title = c("Immigrants five times better off in Britain - Daily Star", 
"Immigrants five times better off in Britain - Daily Star", "Immigrants five times better off in Britain - Daily Star", 
"Immigrants five times better off in Britain - Daily Star", "Immigrants five times better off in Britain - Daily Star", 
"Immigrants five times better off in Britain - Daily Star", "Immigrants five times better off in Britain - Daily Star", 
"Donald Trump pledges to deport 3 MILLION illegal immigrants from the US - Daily Star", 
"Donald Trump pledges to deport 3 MILLION illegal immigrants from the US - Daily Star", 
"Donald Trump pledges to deport 3 MILLION illegal immigrants from the US - Daily Star", 
"Donald Trump pledges to deport 3 MILLION illegal immigrants from the US - Daily Star", 
"Donald Trump pledges to deport 3 MILLION illegal immigrants from the US - Daily Star", 
"Donald Trump pledges to deport 3 MILLION illegal immigrants from the US - Daily Star", 
"Donald Trump pledges to deport 3 MILLION illegal immigrants from the US - Daily Star", 
"Donald Trump pledges to deport 3 MILLION illegal immigrants from the US - Daily Star", 
"Donald Trump pledges to deport 3 MILLION illegal immigrants from the US - Daily Star", 
"Donald Trump pledges to deport 3 MILLION illegal immigrants from the US - Daily Star", 
"Donald Trump pledges to deport 3 MILLION illegal immigrants from the US - Daily Star", 
"Donald Trump pledges to deport 3 MILLION illegal immigrants from the US - Daily Star", 
"Donald Trump pledges to deport 3 MILLION illegal immigrants from the US - Daily Star", 
"Donald Trump pledges to deport 3 MILLION illegal immigrants from the US - Daily Star", 
"Donald Trump pledges to deport 3 MILLION illegal immigrants from the US - Daily Star", 
"Thousands of immigrants get access to state handouts on arrival due to EU loophole - Daily Star", 
"Thousands of immigrants get access to state handouts on arrival due to EU loophole - Daily Star", 
"Thousands of immigrants get access to state handouts on arrival due to EU loophole - Daily Star", 
"Thousands of immigrants get access to state handouts on arrival due to EU loophole - Daily Star", 
"Thousands of immigrants get access to state handouts on arrival due to EU loophole - Daily Star", 
"Thousands of immigrants get access to state handouts on arrival due to EU loophole - Daily Star", 
"Thousands of immigrants get access to state handouts on arrival due to EU loophole - Daily Star", 
"Thousands of immigrants get access to state handouts on arrival due to EU loophole - Daily Star"
), Content = c("IMMIGRANTS from Romania and Bulgaria would be five times better off if they moved to Britain.", 
"Don't miss a thing by getting the Daily Star's biggest headlines straight to your inbox!", 
"Related content", "And families with two kids would be nine times richer, according to shock new figures.", 
"From 2014, the 29 million citizens of Romania and Bulgaria become eligible to live anywhere in Europe – and there are fears that millions will be heading to the UK.", 
"Migration Watch UK says our minimum wage of £254 a week compares to an average £55 a week in those countries.", 
"Chairman Sir Andrew Green said: “Given the incentives, it would be absurd to suggest that there will not be a significant inflow.”", 
"US President-elect Donald Trump has reaffirmed plans to deport millions of illegal immigrants from America in a bold statement to the world.", 
"Don't miss a thing by getting the Daily Star's biggest headlines straight to your inbox!", 
"The 70-year-old billionaire will promise to tackle criminals who were illegally living in America in a broadcast due to be aired later this evening.", 
"Appearing in his first tv interview since his shocking election win, Trump said that two to three million immigrants with criminal records in the US would either be jailed or deported.", 
"He told CBS show 60 Minutes: \"What we are going to do is get the people that are criminal and have criminal records, gang members, drug dealers. where a lot of these people, probably two million, it could even be three million, we are getting them out of our country, they're here illegally.", 
"\"After the border is secure and after everything gets normalised, we're going to make a determination on the people that they're talking about who are terrific people, they're terrific people, but we are gonna (sic) make a determination at that.", 
"\"But before we make that determination, it's very important, we are going to secure our border.\"", 
"Trump also confirmed plans were underway to construct a \"great wall\" on the US-Mexican border.", 
"A spokeswoman for Mr Trump yesterday confirmed that the 70-year-old tycoon had set up a taskforce to begin plans on constructing the wall, which could cost as much as £9.3billion.", 
"But the President-elect did concede that parts of the wall may have to be a fence.", 
"When asked if he would accept a fence, Trump said: \"For certain areas I would, but certain areas, a wall is more appropriate. I’m very good at this, it’s called construction.\"", 
"Congressman Louie Gohmert confirmed yesterday that Trump's wall would is only likely to stretch for “around half” the length of the border, which spans California, Arizona, New Mexico and Texas.", 
"Plans to build the wall has seen widespread protests across the US, with demonstrators taking to the streets to protest about their new president.", 
"Scores have been arrested and a man was shot in Portland, Oregon, following an argument between activists.", 
"In Los Angeles, officers were scouring the route of an earlier protest after an undercover officer lost his gun and handcuffs during a scuffle.", 
"THOUSANDS of immigrants are getting access to UK state handouts as soon as they arrive thanks to an EU loophole.", 
"Don't miss a thing by getting the Daily Star's biggest headlines straight to your inbox!", 
"Related content", "In the past five years 100,000 wives, husbands and children of EU citizens have moved to Britain under a lax system that bypasses rules for Brits.", 
"British people who want close family from outside Europe to move to the UK have to prove they earn around £18,000 a year before they get visas.", 
"But separate rules for EU citizens mean they do not have to bring in the same wages before flying in relatives. They then get the same right to benefits as unemployed Brits.", 
"Sir Andrew Green, chairman of Migration Watch, said: “This is a loophole that must be closed.", 
"“It is absurd that EU citizens should be in a more favourable position than our own citizens.”"
)), row.names = c(NA, -30L), class = c("tbl_df", "tbl", "data.frame"
))

Thanks

PS.: Sorry for the images, the system did not allow me to add actual tables.


Solution

  • We can use

    aggregate(Text ~ Book, df1, FUN = paste, collapse =' ')
    

    -output

     Book          Text
    1 Book1 Text.a Text.b
    2 Book2 Text.c Text.d
    

    For the OP's data

    aggregate( Content ~ Title, df1, FUN = paste, collapse =' ')
    

    -output

                                                                                             Title
    1            Donald Trump pledges to deport 3 MILLION illegal immigrants from the US - Daily Star
    2                                        Immigrants five times better off in Britain - Daily Star
    3 Thousands of immigrants get access to state handouts on arrival due to EU loophole - Daily Star
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    Content
    1 US President-elect Donald Trump has reaffirmed plans to deport millions of illegal immigrants from America in a bold statement to the world. Don't miss a thing by getting the Daily Star's biggest headlines straight to your inbox! The 70-year-old billionaire will promise to tackle criminals who were illegally living in America in a broadcast due to be aired later this evening. Appearing in his first tv interview since his shocking election win, Trump said that two to three million immigrants with criminal records in the US would either be jailed or deported. He told CBS show 60 Minutes: "What we are going to do is get the people that are criminal and have criminal records, gang members, drug dealers. where a lot of these people, probably two million, it could even be three million, we are getting them out of our country, they're here illegally. "After the border is secure and after everything gets normalised, we're going to make a determination on the people that they're talking about who are terrific people, they're terrific people, but we are gonna (sic) make a determination at that. "But before we make that determination, it's very important, we are going to secure our border." Trump also confirmed plans were underway to construct a "great wall" on the US-Mexican border. A spokeswoman for Mr Trump yesterday confirmed that the 70-year-old tycoon had set up a taskforce to begin plans on constructing the wall, which could cost as much as £9.3billion. But the President-elect did concede that parts of the wall may have to be a fence. When asked if he would accept a fence, Trump said: "For certain areas I would, but certain areas, a wall is more appropriate. I’m very good at this, it’s called construction." Congressman Louie Gohmert confirmed yesterday that Trump's wall would is only likely to stretch for “around half” the length of the border, which spans California, Arizona, New Mexico and Texas. Plans to build the wall has seen widespread protests across the US, with demonstrators taking to the streets to protest about their new president. Scores have been arrested and a man was shot in Portland, Oregon, following an argument between activists. In Los Angeles, officers were scouring the route of an earlier protest after an undercover officer lost his gun and handcuffs during a scuffle.
    2                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   IMMIGRANTS from Romania and Bulgaria would be five times better off if they moved to Britain. Don't miss a thing by getting the Daily Star's biggest headlines straight to your inbox! Related content And families with two kids would be nine times richer, according to shock new figures. From 2014, the 29 million citizens of Romania and Bulgaria become eligible to live anywhere in Europe – and there are fears that millions will be heading to the UK. Migration Watch UK says our minimum wage of £254 a week compares to an average £55 a week in those countries. Chairman Sir Andrew Green said: “Given the incentives, it would be absurd to suggest that there will not be a significant inflow.”
    3                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               THOUSANDS of immigrants are getting access to UK state handouts as soon as they arrive thanks to an EU loophole. Don't miss a thing by getting the Daily Star's biggest headlines straight to your inbox! Related content In the past five years 100,000 wives, husbands and children of EU citizens have moved to Britain under a lax system that bypasses rules for Brits. British people who want close family from outside Europe to move to the UK have to prove they earn around £18,000 a year before they get visas. But separate rules for EU citizens mean they do not have to bring in the same wages before flying in relatives. They then get the same right to benefits as unemployed Brits. Sir Andrew Green, chairman of Migration Watch, said: “This is a loophole that must be closed. “It is absurd that EU citizens should be in a more favourable position than our own citizens.”
    

    Or this can be done in tidyverse

    library(dplyr)
    library(stringr)
    df1 %>%
       group_by(Title) %>%
       summarise(Content = str_c(Content, collapse=" "), .groups = 'drop')
    

    data

    df1 <- structure(list(Book = c("Book1", "Book1", "Book2", "Book2"), 
        Text = c("Text.a", "Text.b", "Text.c", "Text.d")), 
    class = "data.frame", row.names = c(NA, 
    -4L))