unire datafame a più liste

ho la seguente lista:

$df1
  x   y          z
1 a 145 2010-01-01
2 b  22 2010-02-01
3 c  33 2010-03-01

$df2
  x   y          z
1 d  45 2010-01-01
2 e  35 2010-02-01
3 f 633 2010-03-01

ho anche un frame di dati che sembra il seguente:

    x   y          z
1 aty 451 2010-01-01
2 b43 452 2010-02-01
3 crf 343 2010-03-01

sto cercando di unire il dataframe di date di colonna z a ciascuna delle liste in modo che l’output previsto sarebbe qualcosa di simile:

$df1
  x   y          z  x.1 y.1
1 a 145 2010-01-01  aty 451
2 b  22 2010-02-01  b43 452
3 c  33 2010-03-01  crf 343

$df2
  x   y          z  x.1 y.1
1 d  45 2010-01-01  aty 451
2 e  35 2010-02-01  b43 452
3 f 633 2010-03-01  crf 343

dati: dati:

foo <- list(df1 = data.frame(x=c('a', 'b', 'c'),y = c(145,22,33), z=c("2010-01-01", "2010-02-01", "2010-03-01")), 
            df2 = data.frame(x=c('d', 'e', 'f'),y = c(45,35,633), z=c("2010-01-01", "2010-02-01", "2010-03-01")))


foo2 <- data.frame(x = c('aty', 'b43', 'crf'), y= c(451,452,343), z=c("2010-01-01", "2010-02-01", "2010-03-01"))

EN From: Join dataframe to multiple lists

More similar articles:

2 Comments

  1. un’opzione di base R è quella di utilizzare Map e merge ogni datafame in foo con foo2

    Map(merge, foo, list(foo2), by = "z")
    
    #$df1
    #           z x.x y.x x.y y.y
    #1 2010-01-01   a 145 aty 451
    #2 2010-02-01   b  22 b43 452
    #3 2010-03-01   c  33 crf 343
    
    #$df2
    #           z x.x y.x x.y y.y
    #1 2010-01-01   d  45 aty 451
    #2 2010-02-01   e  35 b43 452
    #3 2010-03-01   f 633 crf 343
  2. possiamo usare tidyverse

    library(tidyverse)
    map(foo, inner_join, foo2, by = 'z')
    #$df1
    #  x.x y.x          z x.y y.y
    #1   a 145 2010-01-01 aty 451
    #2   b  22 2010-02-01 b43 452
    #3   c  33 2010-03-01 crf 343
    
    #$df2
    #  x.x y.x          z x.y y.y
    #1   d  45 2010-01-01 aty 451
    #2   e  35 2010-02-01 b43 452
    #3   f 633 2010-03-01 crf 343

    o utilizzando base R

    lapply(foo, merge, foo2, by = 'z')
    #$df1
    #           z x.x y.x x.y y.y
    #1 2010-01-01   a 145 aty 451
    #2 2010-02-01   b  22 b43 452
    #3 2010-03-01   c  33 crf 343
    
    #$df2
    #           z x.x y.x x.y y.y
    #1 2010-01-01   d  45 aty 451
    #2 2010-02-01   e  35 b43 452
    #3 2010-03-01   f 633 crf 343

Leave a Reply

Your email address will not be published. Required fields are marked *