[R] Extract values of a two-factor table and duplicate them into a three-factor table

Ista Zahn istazahn at gmail.com
Wed Mar 10 13:28:06 CET 2010


Hi Melanie,
I think you just need

merge(TABLE1, TABLE2)

see ?merge for details.

-Ista

On Wed, Mar 10, 2010 at 12:47 AM, Melanie HAMEL <melanie.hamel at ird.fr> wrote:
> Dear all,
>
> I would like to solve a trivial problem (I guess it is) but can't find the right way. Maybe someone can help me ?
>
> I've got a table with two factors (station = station ID, buffer = buffer size in meters) and a value for each unique combination of those two factors (S = number of habitats within each buffer around each station) like this:
>
> TABLE 1
>
> station buffer S
>
> Abaia01 200 2
> Abaia01 400 2
> Abaia01 600 2
> Abaia01 800 2
> Abaia01 1000 2
> Abaia01 1200 2
> Abaia01 1400 2
> Abaia01 1600 2
> Abaia01 1800 2
> Abaia01 2000 2
> Abaia02 200 2
> Abaia02 400 2
> Abaia02 600 2
> Abaia02 800 2
> Abaia02 1000 2
> Abaia02 1200 2
> Abaia02 1400 2
> Abaia02 1600 2
> Abaia02 1800 3
> Abaia02 2000 3
>
>
> etc..
>
> I would like to fill this more complete table here wich has a third factor "L5_code" (L5_code = type of habitat). As you can see, there are several types of habitats within each buffer around a station (up to several dozens).
>
> TABLE 2
>
> station buffer L5_CODE S
>
> Abaia01 200 14 2
> Abaia01 200 11 2
>
> Abaia01 400 14 2
> Abaia01 400 11 2
>
>
> Abaia01 600 14 2
> Abaia01 600 11 2
>
>
> ... ... ...
>
>
> Abaia02 200 14 2
> Abaia02 200 11 2
>
>
> Abaia02 400 14 2
> Abaia02 400 11 2
>
>
> Abaia02 600 14 2
> Abaia02 600 11 2
>
> I was hoping there was a simple function to do that but for now I've been useless with bad for loops, etc.
>
> One method could be to go go through TABLE 1 step by step then fill TABLE 2 according to a combination of station and buffer of TABLE 1. I.e. go through the first column of TABLE 2 and find the corrseponding station name from TABLE 1, then go through the second column of TABLE 2 and find the corresponding buffer size from TABLE 1, then rep() the value of TABLE 1 in the column S of TABLE 2 corresponding to the right combination...
>
> One other (quick) method would be to create a unique factor from the two first columns of TABLE 1 (station_buffer) but maybe there is something cleaner ? Do you have an idea ?
>
> Thanks !
>
> Mel
>
> PS: those tables are supposed to be very large datasets of several thousands of rows.
>
>
> ___________________________________________________
>
> Mélanie HAMEL
> Institut de Recherche pour le Développement (I.R.D.)
> U227 - CoRéUs (Biocomplexité des Ecosystèmes Coralliens de l'Indo-Pacifique)
>
> Centre IRD - Nouméa
> 101 Promenade Roger Laroque
> Anse Vata
> BP A5 - 98848 Noumea
> New Caledonia
>
> Tel: 26 07 19
> E-mail: melanie.hamel at ird.fr
> Web: http://www.coreus.ird.fr
>
> _______________________________________________________________________
> Save a tree, please don't print this email unless you really need to...
>
>        [[alternative HTML version deleted]]
>
>
> ______________________________________________
> 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.
>
>



-- 
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org



More information about the R-help mailing list