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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:54:22+00:00 2026-06-01T03:54:22+00:00

I have the following data.frames: a <- data.frame(id = 1:3, v1 = c(‘a’, NA,

  • 0

I have the following data.frames:

a <- data.frame(id = 1:3, v1 = c('a', NA, NA), v2 = c(NA, 'b', 'c'))
b <- data.frame(id = 1:3, v1 = c(NA, 'B', 'C'), v2 = c("A", NA, NA))
> a
  id   v1   v2
1  1    a <NA>
2  2 <NA>    b
3  3 <NA>    c
> b
  id   v1   v2
1  1 <NA>    A
2  2    B <NA>
3  3    C <NA>

note: There are no ids for which v1 or v2 are defined in both tables; there is only a single unique non-NA value in each column for each id value

I would like to merge these data frames on matching values of “id’:

ab <- merge(a, b, by = "id")

but I would also like to combine the two columns v1 and v2, so that the data.frame ab will look like this:

ab <- data.frame(id = 1:3, v1 = c("a", "B", "C"), v2 = c("A", "b", "c"))

> ab
  id v1 v2
1  1  a  A
2  2  B  b
3  3  C  c

instead, I get this:

> merge(a, b, by = "id")
  id v1.x v2.x v1.y v2.y
1  1    a <NA> <NA>    A
2  2 <NA>    b    B <NA>
3  3 <NA>    c    C <NA>

it would be helpful to have examples using both data.frame and data.table, so here are the data.table versions of above:

A <- data.table(a, key = 'id')
B <- data.table(b, key = 'id')
A[B]
  • 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-01T03:54:24+00:00Added an answer on June 1, 2026 at 3:54 am

    The type of merge you specify probably won’t be possible using merge (with data frames), although saying that usually invites being proved wrong.

    You also omit some details: will there always be a single unique non-NA value in each column for each id value? If so, this will work:

    ab <- rbind(a,b)
    > colFun <- function(x){x[which(!is.na(x))]}
    > ddply(ab,.(id),function(x){colwise(colFun)(x)})
      id v1 v2
    1  1  a  A
    2  2  B  b
    3  3  C  c
    

    A similar strategy should work with data.tables as well:

    abDT <- data.table(ab,key = "id")
    > abDT[,list(colFun(v1),colFun(v2)),by = id]
         id V1 V2
    [1,]  1  a  A
    [2,]  2  B  b
    [3,]  3  C  c
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following question: I have data frame which looks like this. I
Suppose I have the following data frame: Data1 X1 X2 1 15 1 2
I have the following data test<-data.frame(group=1:10, var.a=rnorm(n=10,mean=500,sd=20), var.b=runif(10)) I would like a barplot with
I have the following data: a=c(1:10) b=c(16:25) c=c(24:33) wa=c(3,7,3,3,3,3,3,3,3,1) wb=c(3,2,3,3,3,3,3,3,3,8) wc=c(4,1,4,4,4,4,4,4,4,1) z=data.frame(a,b,c,wa,wb,wc) I want
I have the following dummy code: dt<-data.frame(country=letters[1:20],val=rnorm(20),siz=rnorm(20)) qplot(x=country,y=val,data=dt,geom=point,size=siz) Now I want to increase the
I'm struggling with the following. If have a (big) data frame with the following:
I have a large data frame with the following fields (example data). #dput(data) gives...
I have the following matching problem: I have two data.frames, one with an observation
I have the following data frame z x y 1 1 a 2 2
i have the following data frame dd <- data.frame(b = c(High, Medium, Highest, Low,

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.