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 9242605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:36:53+00:00 2026-06-18T08:36:53+00:00

I have a data frame that looks like this: _________________id ________________text______ 1 | 7821

  • 0

I have a data frame that looks like this:

_________________id ________________text______
    1   | 7821             | "some text here"
    2   | 7821             |  "here as well"
    3   | 7821             |  "and here"
    4   | 567              |   "etcetera"
    5   | 567              |    "more text"
    6   | 231              |   "other text"

And I would like to group the texts by IDs, so I can run a clustering algorithm:

________________id___________________text______
    1   | 7821             | "some text here here as well and here"
    2   | 567              |   "etcetera more text"
    3   | 231              |   "other text"

Is there any way to do this? I am importing from a database table and I have a lot of data, so I can’t do it manually.

  • 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-06-18T08:36:54+00:00Added an answer on June 18, 2026 at 8:36 am

    You’re actually looking for aggregate, not merge, and there should be many examples on SO demonstrating different options for aggregation. Here’s the most basic and direct approach, using the formula approach to specify which columns to aggregate.

    Here’s your data in a copy-and-pasteable form

    mydata <- structure(list(id = c(7821L, 7821L, 7821L, 567L, 567L, 231L), 
        text = structure(c(6L, 3L, 1L, 2L, 4L, 5L), .Label = c("and here", 
        "etcetera", "here as well", "more text", "other text", "some text here"
        ), class = "factor")), .Names = c("id", "text"), class = "data.frame", 
        row.names = c(NA, -6L))
    

    Here’s the aggregated output.

    aggregate(text ~ id, mydata, paste, collapse = " ")
    #     id                                 text
    # 1  231                           other text
    # 2  567                   etcetera more text
    # 3 7821 some text here here as well and here
    

    Of course, there is also data.table, which has nice compact syntax (and awesome speed):

    > library(data.table)
    > DT <- data.table(mydata)
    > DT[, paste(text, collapse = " "), by = "id"]
         id                                   V1
    1: 7821 some text here here as well and here
    2:  567                   etcetera more text
    3:  231                           other text
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data.frame that looks like this: > head(ff.df) .id pio caremgmt prev
I have a data.frame in R that looks like this: score rms template aln_id
I have a data frame in R that looks like this: > TimeOffset, Source,
I have a data frame that looks like this: Chr start stop ref alt
I have a data.frame that looks like this > head(df) Memory Memory Memory Memory
I'm in a situation like this: I have a data.frame that looks like the
I have a data frame that looks like this: type created_at repository_name 1 IssuesEvent
I have the following data frame (info) that looks like this: > info[1:5,] field
I have a data frame that looks like this, with two key columns and
I have an R data frame that looks like this: z = as.data.frame(list(Col1=c(a,c,e,g),Col2=c(b,d,f,h),Col3=c(1,2,5,3,5,7,9,8,1))) >

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.