Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merging data frames of different row length in R [duplicate]

Tags:

r

plyr

Hello I have been looking for a solution for quite some time. I'm sure the answer is easy but I've been pulling my hair out here!

I have two data frames that are similar (in fact one represents a more complete dataset). They both have two columns, one containing string values as a factor and one containing numerical values.

df.A looks like this:

Category     Number
A            1
B            2
C            3
D            4

and df.B looks like this

Category     Number
A            5
B            6
C            7

These categories (ABCD) are common between the two dataframes. In trying to get df.B to have a category D with a NA or 0 value (I am working with percentages so either NA or 0 is fine), my code looks like this:

proto <- df.A
proto$number <- NULL
df.B <- rbind.fill(proto,df.B)

My thought is this would add the fourth row for category D and give NA value but instead results in

Category     Number
A             NA
B             NA
C             NA
D             NA
NA            5
NA            6
NA            7

I tried removing the factor class from category on both df.A and df.B, tried using rbind.fill.matrix instead...to be honest I am pretty new to R and this is giving me a lot of trouble. How do I get R to recognize that ABCD are the same factor across dataframes?

like image 508
Andrew Avatar asked Jan 24 '26 20:01

Andrew


1 Answers

You can achieve the desired result by using merge:

merge(df.A,df.B,by='Category',all=T)

which will produce the following output:

#  Category Number.x Number.y
#1        A        1        5
#2        B        2        6
#3        C        3        7
#4        D        4       NA
like image 169
Marat Talipov Avatar answered Jan 26 '26 12:01

Marat Talipov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!