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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:49:45+00:00 2026-06-08T20:49:45+00:00

I have a problem and I would like to apologize, if this problem has

  • 0

I have a problem and I would like to apologize, if this problem has been already discussed, eventhough I checked old postings.

I have a data.frame with 2 columns, the second column can contain several identifier, but the number can vary. In another data.frame the identifiers corresponds to another identifier.

df.1  

color   identifier
blue    A1, B2, C3, C4 
yellow  B2, C4, C6
green   A3

df.2

A1 Mercedes
A3 BMW
B2 Porsche
C3 Toyota
C4 Hundai
C5 Volkswagen
C6 Peugeot    

What I would like to have is a data.frame like this:

df.3

color   identifier        identifier2
blue    A1, B2, C3, C4    Mercedes, Porsche, Toyota, Hundai 
yellow  B2, C4, C6        Porsche, Hundai, Peugeot
green   A3                BMW

A data.frame which contains the identifiers and additionally the identifiers of the second data.frame.

I was trying to use apply and stack and unstack, but I was not successful at all.

Do you have any suggestions?

  • 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-08T20:49:47+00:00Added an answer on June 8, 2026 at 8:49 pm

    Here is another solution, using strsplit:

    # The data
    df.1  = read.table(header=TRUE, text="
    color   identifier
    blue    'A1, B2, C3, C4'
    yellow  'B2, C4, C6'
    green   'A3'", stringsAsFactors = FALSE)
    
    df.2 = read.table(header=FALSE, text="
    A1 Mercedes
    A3 BMW
    B2 Porsche
    C3 Toyota
    C4 Hundai
    C5 Volkswagen
    C6 Peugeot", stringsAsFactors=FALSE)
    names(df.2) = c("identifier", "car")
    
    df.1$identifier = strsplit(df.1$identifier, split=", ")
    df.1$identifier1 = lapply(1:nrow(df.1), 
             function(x) df.2[which(df.2$identifier %in% df.1$identifier[[x]]), 2])
    df.1
    #    color     identifier                       identifier1
    # 1   blue A1, B2, C3, C4 Mercedes, Porsche, Toyota, Hundai
    # 2 yellow     B2, C4, C6          Porsche, Hundai, Peugeot
    # 3  green             A3                               BMW
    

    Note that identifier and identifier1 are now lists in your data.frame. I personally find this easier to work with later on.

    str(df.1)
    # 'data.frame':  3 obs. of  3 variables:
    #   $ color      : chr  "blue" "yellow" "green"
    # $ identifier :List of 3
    #  ..$ : chr  "A1" "B2" "C3" "C4"
    #  ..$ : chr  "B2" "C4" "C6"
    #  ..$ : chr "A3"
    # $ identifier1:List of 3
    #  ..$ : chr  "Mercedes" "Porsche" "Toyota" "Hundai"
    #  ..$ : chr  "Porsche" "Hundai" "Peugeot"
    #  ..$ : chr "BMW"
    

    You may need to modify the strsplit if there is any whitespace left over, but it works with this sample data. Also, for strsplit to work, the data need to be in mode as.character (hence my use of stringsAsFactors when reading in the data).

    Update: write.table()

    I do prefer to keep the data in lists in case I wanted to do further analysis. However, if the data are complete or just for output purposes, you might want to do something like this:

    df.3 = df.1
    df.3$identifier = sapply(df.3$identifier, paste0, collapse=", ")
    df.3$identifier1 = sapply(df.3$identifier1, paste0, collapse=", ")
    

    This will allow you to use write.table since identifier and identifier1 are now of mode character instead of list.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem i would like parallelize two for loops with openmp. how
i have the following problem: i would like to create a footer. that footer
Ok, I have the following problem: I would like to scroll an overflowing ListBox
I have a little problem where I would like to insert a svn diff
I have a reasonably complex layout problem: I would like to have a main
I have a problem, I would like to build logging system which will be
I have a problem with apache2 settings (Ubuntu system). I would like to run
I have an interesting genetics problem that I would like to solve in native
I have got very big problem because I would like to get more information
I have come across a strange problem which I would like to get your

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.