Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7695609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:34:58+00:00 2026-05-31T21:34:58+00:00

I have a text variable and a grouping variable. I’d like to collapse the

  • 0

I have a text variable and a grouping variable. I’d like to collapse the text variable into one string per row (combine) by factor. So as long as the group column says m I want to group the text together and so on. I provided a sample data set before and after. I am writing this for a package and have thus far avoided all reliance on other packages except for wordcloudand would like to keep it this way.

I suspect rle may be useful with cumsum but haven’t been able to figure this one out.

Thank you in advance.

What the data looks like

                                 text group
1       Computer is fun. Not too fun.     m
2               No its not, its dumb.     m
3              How can we be certain?     f
4                    There is no way.     m
5                     I distrust you.     m
6         What are you talking about?     f
7       Shall we move on?  Good then.     f
8 Im hungry.  Lets eat.  You already?     m

What I’d like the data to look like

                                                       text group
1       Computer is fun. Not too fun. No its not, its dumb.     m
2                                    How can we be certain?     f
3                          There is no way. I distrust you.     m
4 What are you talking about? Shall we move on?  Good then.     f
5                       Im hungry.  Lets eat.  You already?     m

The Data

dat <- structure(list(text = c("Computer is fun. Not too fun.", "No its not, its dumb.", 
"How can we be certain?", "There is no way.", "I distrust you.", 
"What are you talking about?", "Shall we move on?  Good then.", 
"Im hungry.  Lets eat.  You already?"), group = structure(c(2L, 
2L, 1L, 2L, 2L, 1L, 1L, 2L), .Label = c("f", "m"), class = "factor")), .Names = c("text", 
"group"), row.names = c(NA, 8L), class = "data.frame")

EDIT: I found I can add unique column for each run of the group variable with:

x <- rle(as.character(dat$group))[[1]]
dat$new <- as.factor(rep(1:length(x), x))

Yielding:

                                 text group new
1       Computer is fun. Not too fun.     m   1
2               No its not, its dumb.     m   1
3              How can we be certain?     f   2
4                    There is no way.     m   3
5                     I distrust you.     m   3
6         What are you talking about?     f   4
7       Shall we move on?  Good then.     f   4
8 Im hungry.  Lets eat.  You already?     m   5
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T21:34:59+00:00Added an answer on May 31, 2026 at 9:34 pm

    This makes use of rle to create an id to group the sentences on. It uses tapply along with paste to bring the output together

    ## Your example data
    dat <- structure(list(text = c("Computer is fun. Not too fun.", "No its not, its dumb.", 
    "How can we be certain?", "There is no way.", "I distrust you.", 
    "What are you talking about?", "Shall we move on?  Good then.", 
    "Im hungry.  Lets eat.  You already?"), group = structure(c(2L, 
    2L, 1L, 2L, 2L, 1L, 1L, 2L), .Label = c("f", "m"), class = "factor")), .Names = c("text", 
    "group"), row.names = c(NA, 8L), class = "data.frame")
    
    
    # Needed for later
    k <- rle(as.numeric(dat$group))
    # Create a grouping vector
    id <- rep(seq_along(k$len), k$len)
    # Combine the text in the desired manner
    out <- tapply(dat$text, id, paste, collapse = " ")
    # Bring it together into a data frame
    answer <- data.frame(text = out, group = levels(dat$group)[k$val])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some text that is stored in a variable like this: <div class=foo>text
I have text file with a row like this: SendersTimeSeriesIdentification: COMPANY_A/COMPANY_B/REF_7/20090505 I'd like to
I have the below contents from a .txt file stored into a string variable
Suppose I have a text variable $$string . How can I write a boolean
I have a select control, and in a javascript variable I have a text
I have a paragraph of text in a javascript variable called 'input_content' and that
I have a variable that im define with another variable and some text. $title
I have a GUID variable and I want to write inside a text file
If I have a variable that contains text information (say taken from a textarea),
What I have is following: <xsl:variable name=myvar select=.//spss:category[((not @varName) or @varName=$colVarName) and @text=$series]/spss:cell/@text/> What

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.