Tutorial sobre como unir Data Frames con el comando merge() y reduce() en R.

Puedes seguir el tutorial por vídeo en https://youtu.be/KUK4gHwA4HA

Creamos un par de DF para usarlos en el ejemplo.

Df1 = data.frame (
    Cultivo = c("Trigo", "Melocoton", "Sandia", "Tomate", "Cebada"),
    Grupo = c("Cereal", "Frutal", "Horticola", "Horticola", "Cereal"))
Df2 = data.frame (
    Cultivo = c("Ciruelo", "Melocoton", "Sandia", "Tomate", "Trigo"),
    Agua = c("Regadio", "Regadio", "Regadio", "Regadio", "Secano"))
Df1
##     Cultivo     Grupo
## 1     Trigo    Cereal
## 2 Melocoton    Frutal
## 3    Sandia Horticola
## 4    Tomate Horticola
## 5    Cebada    Cereal
Df2
##     Cultivo    Agua
## 1   Ciruelo Regadio
## 2 Melocoton Regadio
## 3    Sandia Regadio
## 4    Tomate Regadio
## 5     Trigo  Secano

Unimos los DF mediante el comando merge().

merge (Df1, Df2, by = "Cultivo")
##     Cultivo     Grupo    Agua
## 1 Melocoton    Frutal Regadio
## 2    Sandia Horticola Regadio
## 3    Tomate Horticola Regadio
## 4     Trigo    Cereal  Secano
merge (Df1, Df2, by = "Cultivo", sort=F)
##     Cultivo     Grupo    Agua
## 1     Trigo    Cereal  Secano
## 2 Melocoton    Frutal Regadio
## 3    Sandia Horticola Regadio
## 4    Tomate Horticola Regadio
merge (Df1, Df2, by = "Cultivo", all.x=TRUE)
##     Cultivo     Grupo    Agua
## 1    Cebada    Cereal    <NA>
## 2 Melocoton    Frutal Regadio
## 3    Sandia Horticola Regadio
## 4    Tomate Horticola Regadio
## 5     Trigo    Cereal  Secano

Qué ocurre si el campo con datos comunes no tiene el mismo nombre.

Df1 = data.frame (
    Cultivo = c("Cebada", "Melocoton", "Sandia", "Tomate", "Trigo"),
    Grupo = c("Cereal", "Frutal", "Horticola", "Horticola", "Cereal"))
Df2 = data.frame (
    Plantacion = c("Ciruelo", "Melocoton", "Sandia", "Tomate", "Trigo"),
    Agua = c("Regadio", "Regadio", "Regadio", "Regadio", "Secano"))
Df1
##     Cultivo     Grupo
## 1    Cebada    Cereal
## 2 Melocoton    Frutal
## 3    Sandia Horticola
## 4    Tomate Horticola
## 5     Trigo    Cereal
Df2
##   Plantacion    Agua
## 1    Ciruelo Regadio
## 2  Melocoton Regadio
## 3     Sandia Regadio
## 4     Tomate Regadio
## 5      Trigo  Secano

Los unimos.

merge (Df1, Df2, by.x="Cultivo", by.y="Plantacion")
##     Cultivo     Grupo    Agua
## 1 Melocoton    Frutal Regadio
## 2    Sandia Horticola Regadio
## 3    Tomate Horticola Regadio
## 4     Trigo    Cereal  Secano
merge (Df1, Df2, by.x="Cultivo", by.y="Plantacion", all=T)
##     Cultivo     Grupo    Agua
## 1    Cebada    Cereal    <NA>
## 2 Melocoton    Frutal Regadio
## 3    Sandia Horticola Regadio
## 4    Tomate Horticola Regadio
## 5     Trigo    Cereal  Secano
## 6   Ciruelo      <NA> Regadio

Unión de tres o mas DF con el comando Reduce().

Df3 = data.frame (
    Cultivo = c("Cebada", "Melocoton", "Sandia", "Tomate", "Trigo","Ciruelo"),
    Terreno = c("Abierto", "Abierto", "Invernadero", "Invernadero", "Abierto", "Abierto"))
Df3
##     Cultivo     Terreno
## 1    Cebada     Abierto
## 2 Melocoton     Abierto
## 3    Sandia Invernadero
## 4    Tomate Invernadero
## 5     Trigo     Abierto
## 6   Ciruelo     Abierto
names(Df2)[1] = "Cultivo"

Reduce(merge, list(Df1,Df2,Df3)) # Arroja un DF con datos en comunes de todos ellos
##     Cultivo     Grupo    Agua     Terreno
## 1 Melocoton    Frutal Regadio     Abierto
## 2    Sandia Horticola Regadio Invernadero
## 3    Tomate Horticola Regadio Invernadero
## 4     Trigo    Cereal  Secano     Abierto
Reduce(function(...) merge (..., all=T), list(Df1,Df2,Df3)) # Resulta un DF con todos los datos
##     Cultivo     Grupo    Agua     Terreno
## 1    Cebada    Cereal    <NA>     Abierto
## 2   Ciruelo      <NA> Regadio     Abierto
## 3 Melocoton    Frutal Regadio     Abierto
## 4    Sandia Horticola Regadio Invernadero
## 5    Tomate Horticola Regadio Invernadero
## 6     Trigo    Cereal  Secano     Abierto

Por último, vemos el ejemplo con los datos del ensayo de patatas que hemos utilizado en vídeos anteriores. Establezco el directorio de trabajo.

setwd("~/Expression/Expression Encoder/Output/32 Unir Data Frames II")

Cargo los datos.

DatosTesPeso = read.table("PotatoTestigoFilOrd2Peso.csv", header=T, sep="," , dec=".")
DatosTesNr = read.table("PotatoTestigoFilOrdNr.csv", header=T, sep="," , dec=".")
DatosTesPeso
##   Tratamiento Variedad Parcela Peso40 Peso41a45 Peso46a60 PesoMas61
## 1     Testigo    Krone       4    130      1120     17180     11780
## 2     Testigo    Krone       1    380      1120     16180      2360
## 3     Testigo   Nicola       6    700       300      8810       420
## 4     Testigo    Krone       3    820       920     14840      6580
## 5     Testigo   Nicola       9    930      1940     11080       500
## 6     Testigo    Krone       5   1840      1240     12580      7040
## 7     Testigo    Krone       2   2000      1480     15600      2340
## 8     Testigo   Nicola       8   2730      1800     11080       500
## 9     Testigo   Nicola       7   3340      3480     12700       680
DatosTesNr
##   Tratamiento Variedad Parcela Numero40 Numero41a45 Numero46a60
## 1     Testigo   Nicola       6       12           5          63
## 2     Testigo   Nicola       9       17          26          92
## 3     Testigo   Nicola       8       34          37          95
## 4     Testigo    Krone       5       22          16         101
## 5     Testigo   Nicola       7       71          47         101
## 6     Testigo    Krone       2       42          25         124
## 7     Testigo    Krone       4        3          16         125
## 8     Testigo    Krone       3       16          13         129
## 9     Testigo    Krone       1        9          23         130
##   NumeroMas61
## 1           2
## 2           2
## 3           2
## 4          30
## 5           3
## 6          14
## 7          44
## 8          28
## 9          16

Uno los DF.

DatosTesTotal = merge(DatosTesPeso, DatosTesNr, by = "Parcela")
DatosTesTotal
##   Parcela Tratamiento.x Variedad.x Peso40 Peso41a45 Peso46a60 PesoMas61
## 1       1       Testigo      Krone    380      1120     16180      2360
## 2       2       Testigo      Krone   2000      1480     15600      2340
## 3       3       Testigo      Krone    820       920     14840      6580
## 4       4       Testigo      Krone    130      1120     17180     11780
## 5       5       Testigo      Krone   1840      1240     12580      7040
## 6       6       Testigo     Nicola    700       300      8810       420
## 7       7       Testigo     Nicola   3340      3480     12700       680
## 8       8       Testigo     Nicola   2730      1800     11080       500
## 9       9       Testigo     Nicola    930      1940     11080       500
##   Tratamiento.y Variedad.y Numero40 Numero41a45 Numero46a60 NumeroMas61
## 1       Testigo      Krone        9          23         130          16
## 2       Testigo      Krone       42          25         124          14
## 3       Testigo      Krone       16          13         129          28
## 4       Testigo      Krone        3          16         125          44
## 5       Testigo      Krone       22          16         101          30
## 6       Testigo     Nicola       12           5          63           2
## 7       Testigo     Nicola       71          47         101           3
## 8       Testigo     Nicola       34          37          95           2
## 9       Testigo     Nicola       17          26          92           2

Las variables que se repiten tras la union tiene un sufijo que indican de qué DF provienen. También podríamos haber hecho la unión por mas de una variable al mismo tiempo.

DatosTesTotal2 = merge(DatosTesPeso, DatosTesNr, by = c("Parcela", "Tratamiento", "Variedad"))
DatosTesTotal2
##   Parcela Tratamiento Variedad Peso40 Peso41a45 Peso46a60 PesoMas61
## 1       1     Testigo    Krone    380      1120     16180      2360
## 2       2     Testigo    Krone   2000      1480     15600      2340
## 3       3     Testigo    Krone    820       920     14840      6580
## 4       4     Testigo    Krone    130      1120     17180     11780
## 5       5     Testigo    Krone   1840      1240     12580      7040
## 6       6     Testigo   Nicola    700       300      8810       420
## 7       7     Testigo   Nicola   3340      3480     12700       680
## 8       8     Testigo   Nicola   2730      1800     11080       500
## 9       9     Testigo   Nicola    930      1940     11080       500
##   Numero40 Numero41a45 Numero46a60 NumeroMas61
## 1        9          23         130          16
## 2       42          25         124          14
## 3       16          13         129          28
## 4        3          16         125          44
## 5       22          16         101          30
## 6       12           5          63           2
## 7       71          47         101           3
## 8       34          37          95           2
## 9       17          26          92           2