silent_hunter
silent_hunter

Reputation: 2508

Merge data frames with dplyr

I have two data frames (MY_DATA and MY_DATA1).First contain ten rows and second contain thirteen rows.You can see data with code below:

MY_DATA<-data.frame(structure(list(TEST = c(10, 20, 30, 40, 50, 60, 70, 80, 90, 
                                               100)), class = "data.frame", row.names = c(NA, -10L)))

MY_DATA1<-data.frame(structure(list(TRAINING= structure(c(1L, 4L, 6L, 8L, 3L, 5L, 
                                                           7L, 9L, 10L, 11L, 12L, 13L, 2L), .Label = c("10", "1000", "200", 
                                                                                                       "30", "300", "40", "400", "50", "500", "600", "700", "800", "900"
                                                           ), class = "factor")), class = "data.frame", row.names = c(NA, 
                                                                                                                      -13L)))

So my intention is to merge this two data frames into one single data frame.Output should be like pic below

enter image description here

I tried with this line of code, but don't work properly.So can anybody help me how to resolve this problem ?

Upvotes: 1

Views: 55

Answers (1)

DavideBrex
DavideBrex

Reputation: 2414

You can do it in this way:

MY_DATA$index = rownames(MY_DATA)
MY_DATA1$index = rownames(MY_DATA1)

dplyr::full_join(MY_DATA, MY_DATA1, by="index")

Output:

   TEST index TRAINING
1    10     1       10
2    20     2       30
3    30     3       40
4    40     4       50
5    50     5      200
6    60     6      300
7    70     7      400
8    80     8      500
9    90     9      600
10  100    10      700
11   NA    11      800
12   NA    12      900
13   NA    13     1000

Upvotes: 1

Related Questions