2016-06-17 31 views
6

Ho un problema complicato: Ho due frame di dati che hanno una combinazione di no e specifica (Colore, Parte) ciascuno per 4 macchine diverse. Vi è un conteggio totale di 5 specifiche per macchina (in realtà anche più macchine e specifiche). Qui ci sono due frame di dati campione:Unisci due frame di dati con tutte le combinazioni

df1 <- data.frame( 
    nr=c("000", "000", "000", "001", "002", 
     "002", "003", "004", "004", "004", "005"), 
    Color=c("Red", "Cyan", "Brown", "Blue", "Red", 
      "Green", "DeepBlue", "Orange", "Cyan", "Grey", "Magenta"), 
    mach1=c(1, NA, NA, 1, NA, 1, 1, NA, 1, NA, 1), 
    mach2=c(1, NA, NA, 1, NA, 1, 1, 1, NA, NA, 1), 
    mach3=c(NA, 1, NA, 1, 1, NA, 1, NA, NA, 1, 1), 
    mach4=c(NA, NA, 1, 1, NA, 1, 1, NA, NA, 1, 1)) 

df2 <- data.frame( 
    nr=c("000", "000", "001", "002", "002", 
     "003", "003", "004", "005", "005"), 
    Part=c("Car", "Tree", "Flag", "Tree", "Road", 
     "Road", "House", "Plane", "House", "Car"), 
    mach1=c(NA, 1, 1, NA, 1, NA, 1, 1, NA, 1), 
    mach2=c(1, NA, 1, NA, 1, NA, 1, 1, 1, NA), 
    mach3=c(NA, 1, 1, 1, NA, 1, NA, 1, 1, NA), 
    mach4=c(NA, 1, 1, 1, NA, 1, NA, 1, 1, NA)) 

Così ho queste uscite:

> df1 
    nr Color mach1 mach2 mach3 mach4 
1 000  Red  1  1 NA NA 
2 000  Cyan NA NA  1 NA 
3 000 Brown NA NA NA  1 
4 001  Blue  1  1  1  1 
5 002  Red NA NA  1 NA 
6 002 Green  1  1 NA  1 
7 003 DeepBlue  1  1  1  1 
8 004 Orange NA  1 NA NA 
9 004  Cyan  1 NA NA NA 
10 004  Grey NA NA  1  1 
11 005 Magenta  1  1  1  1 
> df2 
    nr Part mach1 mach2 mach3 mach4 
1 000 Car NA  1 NA NA 
2 000 Tree  1 NA  1  1 
3 001 Flag  1  1  1  1 
4 002 Tree NA NA  1  1 
5 002 Road  1  1 NA NA 
6 003 Road NA NA  1  1 
7 003 House  1  1 NA NA 
8 004 Plane  1  1  1  1 
9 005 House NA  1  1  1 
10 005 Car  1 NA NA NA 

Ora vorrei combinare questi due frame di dati in un nuovo DF3 cornice di dati che mostra tutte le combinazioni di colori e parte per le macchine specifiche come questa:

> df3 
    nr Color Part mach1 mach2 mach3 mach4 
1 000  Red Tree  1 NA NA NA 
2 000  Red Car NA  1 NA NA 
3 000  Cyan Tree NA NA  1 NA 
4 000 Brown Tree NA NA NA  1 
5 001  Blue Flag  1  1  1  1 
6 002 Green Road  1  1 NA NA 
7 002  Red Tree NA NA  1 NA 
8 002 Green Tree NA NA NA  1 
9 003 Deepblue House  1  1 NA NA 
10 003 Deepblue Road NA NA  1  1 
11 004  Cyan Plane  1 NA NA NA 
12 004 Orange Plane NA  1 NA NA 
13 004  Grey Plane NA NA  1  1 
14 005 Magenta Car  1 NA NA NA 
15 005 Magenta House NA  1  1  1 
> 

Qualche suggerimento?

risposta

6

è possibile innanzitutto rimodellare i due frame di dati (utilizzando melt da reshape2), poi unirle (utilizzando full_join da dplyr) e rimodellare, che di nuovo nel formato originale:

library(dplyr) 
library(reshape2) 

df1.b <- melt(df1) 
df2.b <- melt(df2) 

df3 <- full_join(df1.b, df2.b) 

df3 <- na.omit(df3) 

df3.b <- dcast(df3, nr + Color + Part ~ variable) 

    nr Color Part mach1 mach2 mach3 mach4 
1 000 Brown Tree NA NA NA  1 
2 000  Cyan Tree NA NA  1 NA 
3 000  Red Car NA  1 NA NA 
4 000  Red Tree  1 NA NA NA 
5 001  Blue Flag  1  1  1  1 
6 002 Green Road  1  1 NA NA 
7 002 Green Tree NA NA NA  1 
8 002  Red Tree NA NA  1 NA 
9 003 DeepBlue House  1  1 NA NA 
10 003 DeepBlue Road NA NA  1  1 
11 004  Cyan Plane  1 NA NA NA 
12 004  Grey Plane NA NA  1  1 
13 004 Orange Plane NA  1 NA NA 
14 005 Magenta Car  1 NA NA NA 
15 005 Magenta House NA  1  1  1 
+1

Grazie mille, sembra funzionare come l'inferno ;-)) – Juppes

4

stessa idea @beetroot, implementato utilizzando Base R reshape() e merge():

reshape(na.omit(merge(
    reshape(df1,dir='l',idvar=c('nr','Color'),varying=grep('^mach',names(df1)),v.names='mach'), 
    reshape(df2,dir='l',idvar=c('nr','Part'),varying=grep('^mach',names(df2)),v.names='mach') 
)),dir='w',idvar=c('nr','Color','Part'),sep=''); 
##  nr Color Part mach1 mach2 mach3 mach4 
## 1 000  Red Tree  1 NA NA NA 
## 4 000  Red Car NA  1 NA NA 
## 7 000  Cyan Tree NA NA  1 NA 
## 10 000 Brown Tree NA NA NA  1 
## 13 001  Blue Flag  1  1  1  1 
## 17 002 Green Road  1  1 NA NA 
## 21 002  Red Tree NA NA  1 NA 
## 23 002 Green Tree NA NA NA  1 
## 25 003 DeepBlue House  1  1 NA NA 
## 27 003 DeepBlue Road NA NA  1  1 
## 29 004  Cyan Plane  1 NA NA NA 
## 30 004 Orange Plane NA  1 NA NA 
## 31 004  Grey Plane NA NA  1  1 
## 33 005 Magenta Car  1 NA NA NA 
## 34 005 Magenta House NA  1  1  1 

(. nomi riga possono essere fissati up dopo, se necessario)

Problemi correlati