[R] problem merging data with different shapes

Petr PIKAL petr.pikal at precheza.cz
Mon Dec 5 11:55:22 CET 2011


Hi
> 
> 
> I have been trying to merge datasets, one of which has a long format 
> (Adata) and one has a (different) long format (Bdata):
> 
> Adata                              Bdata
> subject order bpm                  subject order trial agegroup gender
> 1         1   70.2                    1      1     3       2       1
> 1         1   69.5                    1      2     1       2       1
> 1         1   68.8                    1      3     2       2       1 
> 1         2   69.1                    2      1     2       1       2
> 1         2    70                     2      2     3       1       2
> 1         2   70.5                    2      3     1       1       2
> 1         3   70.2                    ...
> 1         3 
> 1         3
> 2         1 
> 2         1 
> ...       ...
> 
> In the end I would like to have a dataset that contains A unchanged with 

> the additional information from B added.
> 
> subject order bpm trial agegroup gender
> 1         1   70.2  3       2       1
> 1         1   69.5  3       2       1 
> 1         1   68.8  3       2       1 
> ... 
> I have tried:
> newdataframe <- merge(Adata,Bdata, by= c("subject", "order"), sort = 
FALSE)

Hm it seems that it should work. Try

newdataframe <- merge(Adata,Bdata, by= c("subject", "order"), all=TRUE)

Regards
Petr

> 
> For some reason, the trial column is not matched to the subject and 
order 
> information, despite them being identified as key-variables for the 
merge.
> (The same is true for other variables, the actual dataset has more 
> variables and trials, but this is essentially the problem.)
> So it results in something like:
> subject order bpm trial agegroup gender
> 1         1   70.2  3       2       1
> 1         1   69.5  2       2       1 
> 1         1   68.8  1       2       1 
> 
> What could be my mistake?
> 
> Thank you VERY much.
> Miriam
> 
> 
> -- 
> 
> 
> 
> 
> --
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list