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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:31:51+00:00 2026-06-16T01:31:51+00:00

I have a data set that looks like this Code Product 1 A|B 2

  • 0

I have a data set that looks like this

Code   Product
1      A|B
2      A|B|C
3      A|B|C|D|E

When I split the column Product using colsplit function, duplication occurs. The output of colsplit function looks like this:

Code  Product.1   Product.2  Product.3  Product.4  Product.5
1     A           B          A          B          A
2     A           B          C          A          B
3     A           B          C          D          E

This happens because one of the cells had five elements. Is there any way to avoid this duplication?

Thanks and regards
Jayaram

  • 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-16T01:31:52+00:00Added an answer on June 16, 2026 at 1:31 am

    Update (21 Oct 2013)

    The concepts below have been rolled into a family of functions called concat.split.* in my “splitstackshape” package. Here is a very straightforward solution using concat.split.multiple:

    library(splitstackshape)
    concat.split.multiple(temp, "Product", "|", "long")
    #    Code time Product
    # 1     1    1       A
    # 2     2    1       A
    # 3     3    1       A
    # 4     1    2       B
    # 5     2    2       B
    # 6     3    2       B
    # 7     1    3    <NA>
    # 8     2    3       C
    # 9     3    3       C
    # 10    1    4    <NA>
    # 11    2    4    <NA>
    # 12    3    4       D
    # 13    1    5    <NA>
    # 14    2    5    <NA>
    # 15    3    5       E
    

    Remove the "long" argument if you want the wide format, but your comments indicated that ultimately you wanted a long format for your output.


    Original answer (17 Dec 2012)

    You can do this with strsplit and sapply as follows:

    # Your data
    temp <- structure(list(Code = 1:3, Product = c("A|B", "A|B|C", "A|B|C|D|E"
    )), .Names = c("Code", "Product"), class = "data.frame", row.names = c(NA, -3L))
    
    temp1 <- strsplit(temp$Product, "\\|") # Split the product cell
    temp1 <- data.frame(Code = temp$Code, 
                        t(sapply(temp1, 
                               function(x) { 
                                   temp <- matrix(NA, 
                                                  nrow = max(sapply(temp1, length)));
                                   temp[1:length(x)] <- x; temp})))
    temp1
    #   Code X1 X2   X3   X4   X5
    # 1    1  A  B <NA> <NA> <NA>
    # 2    2  A  B    C <NA> <NA>
    # 3    3  A  B    C    D    E
    

    Or… use rbind.fill from the “plyr” package, after making each of your rows into a single column data.frame:

    temp1 <- strsplit(temp$Product, "\\|")
    library(plyr)
    data.frame(Code = temp$Code, 
               rbind.fill(lapply(temp1, function(x) data.frame(t(x)))))
    #   Code X1 X2   X3   X4   X5
    # 1    1  A  B <NA> <NA> <NA>
    # 2    2  A  B    C <NA> <NA>
    # 3    3  A  B    C    D    E
    

    Or… inspired by @DWin’s great answer here, re-read the second column as a data.frame in itself.

    newcols <- max(sapply(strsplit(temp$Product, "\\|"), length))
    temp2 <- data.frame(Code = temp$Code,
                        read.table(text = as.character(temp$Product), 
                                   sep="|", fill=TRUE, 
                                   col.names=paste("Product", seq(newcols))))
    temp2
    #   Code Product.1 Product.2 Product.3 Product.4 Product.5
    # 1    1         A         B                              
    # 2    2         A         B         C                    
    # 3    3         A         B         C         D         E
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data set that looks like this: ID | DATE | SCORE
I have a data set that looks like this: [ { Size : Small,
I have a data set that looks like this: 000 100 200 300 010
I have a data set that looks something like this: Gender | Age |
I have a data set that looks something like this: Gender | Age |
I have a set of data that is structured like this: ItemA.GroupA ItemB.GroupA ItemC.GroupB
I have data that always looks something like this: alt text http://michaelfogleman.com/static/images/chart.png I need
All, I have some PHP PDO code that looks like this: <?php ... $query=$dbh->prepare(SELECT
I have data that looks something like this: sensorid | sampletime | correctedvalue |
I have an Oracle table with data that looks like this: ID BATCH STATUS

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.