Skip to content

merge with origin information in new variable names

4 messages · Jeff Newmiller, Eric Fail, Phil Spector

#
Dear R-list,

Here is my simple question,

I have n data frames that I would like to merge, but I can't figure
out how to add information about the origin of the variable(s).

Here is my problem,

DF.wave.1 <- data.frame(id=1:10,var.A=sample(letters[1:4],10,TRUE))
DF.wave.2 <- data.frame(id=1:10,var.M=sample(letters[5:8],10,TRUE))
DF.wave.3 <- data.frame(id=1:10,var.A=sample(letters[5:8],10,TRUE))

Now; I would like to merge the three dataframes into one, but append a
suffix to the individual variables names about thir origin.

DF.wave.all <- merge(DF.wave.1,DF.wave.2,DF.wave.3,by="id", [what to do here])

In other words, I would like it to loook like this.

DF.wave.all
   id var.A.wave.1 var.M.wave.2 var.A.wave.3
1   1            c            h            j
2   2            c            e            j
3   3            c            g            k
4   4            c            e            j
5   5            c            g            i
6   6            d            e            k
7   7            c            h            k
8   8            b            g            j
9   9            b            f            i
10 10            d            h            i


Is there a command I can use directly in merge? 'suffixes' isn't
really handy here.

Thanks,
Eric
#
Is there anyone out there who can suggest a way to solve this problem?

Thanks,
Esben

On Sun, Apr 24, 2011 at 8:53 PM, Jeff Newmiller
<jdnewmil at dcn.davis.ca.us> wrote:
#
Eric -
     As others have said, you should change the names of the variables
in the data frames before you merge them.  Here's one implementation
of that idea:

    DF.wave.1 <- data.frame(id=1:10,var.A=sample(letters[1:4],10,TRUE))
    DF.wave.2 <- data.frame(id=1:10,var.M=sample(letters[5:8],10,TRUE))
    DF.wave.3 <- data.frame(id=1:10,var.A=sample(letters[5:8],10,TRUE))

    nms = paste('wave',1:3,sep='.')
    dfs = list(DF.wave.1,DF.wave.2,DF.wave.3)
    names(dfs) = nms

    changenm = function(nm){
        df = dfs[[nm]]
        wh = names(df) != 'id'
        names(df)[wh] = paste(names(df)[wh],nm,sep='.')
        df
    }

    Reduce(function(x,y)merge(x,y,by='id'),lapply(names(dfs),changenm))

 					- Phil Spector
 					 Statistical Computing Facility
 					 Department of Statistics
 					 UC Berkeley
 					 spector at stat.berkeley.edu
On Mon, 25 Apr 2011, Eric Fail wrote: