3

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?

Andrew
  • 65
  • 6
  • 5
    You need `merge(df.A,df.B,by='Category',all=T)`. Ironically, you already have the word 'merge' in the title of your question :) – Marat Talipov Feb 04 '15 at 17:12
  • 1
    @MaratTalipov that seemed to fix it! Thank you – Andrew Feb 04 '15 at 17:19
  • @DavidArenburg I suppose beginners like me try to jump to a more complicated solution before trying the easy stuff...I guess I have a lot to learn :P – Andrew Feb 04 '15 at 17:23

1 Answers1

2

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
Marat Talipov
  • 13,064
  • 5
  • 34
  • 53