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) 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 -- --
Please use dput() to post your example data sets. dput(Adata) dput(Bdata) **then copy and paste the results of each so that we can "play" around with it easily. Miriam -2 wrote> > 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) > > 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@ 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. >-- View this message in context: http://r.789695.n4.nabble.com/problem-merging-data-with-different-shapes-tp4157137p4157442.html Sent from the R help mailing list archive at Nabble.com.
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 andorder> information, despite them being identified as key-variables for themerge.> (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 guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.