2

So I have two lists:

List X is 5 lines long and takes the form of:

Thing1 1
Thing2 2
Thing3 3
Thing4 4
Thing5 5

List Y is also five lines long:

Thing1 A
Thing2 B
Thing3 C
Thing4 D
Thing5 E

The output I want is a data frame:

Things X Y
Thing1 1 A
Thing2 2 B
Thing3 3 C
Thing4 4 D
Thing5 5 E

In reality there are 500+ "Things" so I can't just name them all in the code.

Help me understand how to do this. I'm utterly lost.

Thanks!

  • 1
    Does this answer your question? [How to join (merge) data frames (inner, outer, left, right)](https://stackoverflow.com/questions/1299871/how-to-join-merge-data-frames-inner-outer-left-right) – camille Nov 22 '19 at 21:39

2 Answers2

0

Simply use merge:

  a <- list(things=c('Things1','Things2','Things3'),Y=c('A','B','C')) 
  b <- list(things=c('Things1','Things2','Things3'),X=c(1,2,3))
  merge(a,b,by='things')


   things  Y  X
1 Things1  A  1
2 Things2  B  2
3 Things3  C  3
Rushabh Patel
  • 2,672
  • 13
  • 34
0

If I am understanding your question I would do a join on the data, using things as the joining value.

library(dplyr)
a <- data.frame( name = c('thing1','thing2','thing3','thing4','thing5'),
             X = 1:5 )
b <- data.frame(name = c('thing1','thing2','thing3','thing4','thing5'),
            Y = c('A','B','C','D','E'))

a %>% inner_join(b , by = c('name'= 'name'))

       name X Y
   1 thing1 1 A
   2 thing2 2 B
   3 thing3 3 C
   4 thing4 4 D
   5 thing5 5 E
Patrick25
  • 121
  • 4