[R] Joining Dataframes

Don MacQueen macq at llnl.gov
Tue Oct 4 22:51:26 CEST 2005


It appears that you want to match rows by date. It also looks like 
your date information is in the row names, only, if the layout of 
your example can be trusted.

The help for merge says this:
     "By default the data frames are merged on the columns with names 
they both have..."
but the column you want to merge by, date, is not in either 
dataframe. So, obviously, it can't merge on date. Also, since your 
example has other columns with the same name that you do not want to 
merge on, you will have to tell it explicitly what columns to use for 
the merge.

So, assuming I've interpreted your example correctly, try this:
   (1) get your dates into a column
   (2) use the 'by' argument of merge to force it to merge on date only.

            merge(species1.effort, species2.effort, by='date')


-Don



At 11:37 AM -0800 10/4/05, Matt Goff wrote:
>I am attempting to join several dataframes that summarize sampling effort 
>for different samples into one large data.frame/table.
>I have looked at the merge command, but have not been clever enough to 
>figure out how to get it to do what I want.
>
>A simplified example of what I am trying to do:
>
>The dataframes I have look like this (they were generated using the table 
>command)
>
>species1.effort
>             site1   site2   site3
>date1     5        5       5
>date2     4        5       5
>date3     4        5       5
>
>species2.effort
>             site1   site2   site3
>date1     7        8        6
>date3     8        7        8
>date4     8        8        8
>
>I would like to join these into a table that looks something like this:
>
>Effort
>              sp1.site1  sp1.site2  sp1.site3  sp2.site1  sp2.site2  
>sp2.site2
>date1         5               5          5             7             
>8            6
>date2         4               5          5            NA          
>NA         NA
>date3         4               5           5           8             
>7             8
>date4         NA           NA        NA         8            8             
>8
>
>
>I have many more dates and species, but I assume whatever method works for 
>this small example, would work for larger cases.
>
>Thanks,
>Matt Goff
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


-- 
--------------------------------------
Don MacQueen
Environmental Protection Department
Lawrence Livermore National Laboratory
Livermore, CA, USA




More information about the R-help mailing list