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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:39:10+00:00 2026-06-12T06:39:10+00:00

I have 2 data frames containing serial numbers for products people have purchased, sorted

  • 0

I have 2 data frames containing serial numbers for products people have purchased, sorted by number of purchases. The first column is the custId and the next 5 columns are the serial numbers, sorted from left to right by number of items purchased.

df1

  id col1 col2 col3 col4 col5
1  1 4742  927 7889   NA   NA
2  2 4964 9295 9174  228 9470
3  3 5834 7758   NA   NA   NA
4  4 2802 9984  323   NA   NA
5  5  179  198 3996 6801 7561
6  6 7755 1252 9684 9940   NA

df2

  id col6 col7 col8 col9 col10
1  1 1816 6686   NA   NA    NA
2  2 6141 9728 6981 3089  5674
3  3 5659 3931 5022 4361  9264
4  4 3210 2488 9939 7543  7757
5  5 9213 1372 4374 7962  4983
6  6 3451 5646 6069   NA    NA

I am trying to consolidate them into a single group of 5 serial numbers that would look like this:

  id col1 col2 col3 col4 col5
1  1 4742  927 7889 1816 6686   
2  2 4964 9295 9174  228 9470
3  3 5834 7758 5022 4361 9264
4  4 2802 9984  323 7543 7757
5  5  179  198 3996 6801 7561
6  6 7755 1252 9684 9940 3451 

There are several issues.

1) How to find unique values in a row.

2) How to maintain order across the row.

Any suggestions?

> dput(df1)
structure(list(id = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), col1 = c(4742, 
4964, 5834, 2802, 179, 7755, 6467, 8671, 2910, 150), col2 = c(927, 
9295, 7758, 9984, 198, 1252, 1664, 5242, 6995, 3875), col3 = c(7889, 
9174, NA, 323, 3996, 9684, 1150, 2973, 9948, 8598), col4 = c(NA, 
228, NA, NA, 6801, 9940, 854, 4744, 4006, 3196), col5 = c(NA, 
9470, NA, NA, 7561, NA, 4342, 1791, 286, 7425)), .Names = c("id", 
"col1", "col2", "col3", "col4", "col5"), row.names = c(NA, -10L
), class = "data.frame")
> dput(df2)
structure(list(id = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), col6 = c(1816, 
6141, 5659, 3210, 9213, 3451, 2440, 5706, 5281, 7110), col7 = c(6686, 
9728, 3931, 2488, 1372, 5646, 2641, 7851, 5581, 5775), col8 = c(NA, 
6981, 5022, 9939, 4374, 6069, 7525, 4927, 9767, 1331), col9 = c(NA, 
3089, 4361, 7543, 7962, NA, 7526, 4215, 9923, 9887), col10 = c(NA, 
5674, 9264, 7757, 4983, NA, 9996, 5886, 9546, 9419)), .Names = c("id", 
"col6", "col7", "col8", "col9", "col10"), row.names = c(NA, -10L
), class = "data.frame")
  • 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-12T06:39:11+00:00Added an answer on June 12, 2026 at 6:39 am

    I think this will work:

    x <- cbind(df1[, -1], df2[, -1])
    dups <- function(x) x[!duplicated(x)]
    new.df <- data.frame(df1[, 1, drop=FALSE], 
        t(apply(x, 1, function(x) dups(na.omit(x))[1:5])))
    
    colnames(new.df)[-1] <- colnames(df1[, -1])
    new.df
    

    Which yields:

       id col1 col2 col3 col4 col5
    1   1 4742  927 7889 1816 6686
    2   2 4964 9295 9174  228 9470
    3   3 5834 7758 5659 3931 5022
    4   4 2802 9984  323 3210 2488
    5   5  179  198 3996 6801 7561
    6   6 7755 1252 9684 9940 3451
    7   7 6467 1664 1150  854 4342
    8   8 8671 5242 2973 4744 1791
    9   9 2910 6995 9948 4006  286
    10 10  150 3875 8598 3196 7425
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two data frames with two columns each. The first column is timestamps
I have a data frame containing 10k rows, for a given column X I
I have 2 lists, and inside each are two more lists containing data frames
In a data frame, I have one column containing character strings. Let's say it
I have a list of data frames, all containing numeric data. How would I
I have following data frames > head(elo) date elo 1 1921-12-18 1597 2 1922-05-14
I have many data.frames, for example: df1 = data.frame(names=c('a','b','c','c','d'),data1=c(1,2,3,4,5)) df2 = data.frame(names=c('a','e','e','c','c','d'),data2=c(1,2,3,4,5,6)) df3 =
I have 2 data frames. One is training data ( pubs1 ), the other
I am using R and I have two data frames: carrots and cucumbers. Each
I'm trying to do some data manipulation in R. I have 2 data frames,

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.