[R] Combining files

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Jan 2 23:05:35 CET 2002


I think indexing is easier here.  Supposing you make the first column the
row names (read with header=T and a header) then

frame1$nature <- frame2[row.names(frame1), 1]

should do it.  Otherwise (not using row names), something like

frame1$nature <- frame2[match(frame1$id, frame2$id), 2]



On Wed, 2 Jan 2002, Laurent Gautier wrote:

>
>
> Would the following make it ?
>
>
> frame1 _ read.table("file1", sep=",")
> n1 _ length(frame1)
> m1 _ rep(F, n1)
> m1[1] _ T
>
> frame2 _ read.table("file2", sep=",")
> ## might a more elegant way to do the following: I remember
> ## discussions about it in this list
> frame2 _ frame2[,1:2]
> m2 _ c(T,F)
>
> my.data.frame _ merge(frame1, frame2, m1, m2, all.x=T)
>
> x
>
>
> Laurent
>
>
> Laurent Gautier			CBS, Building 208, DTU
> PhD. Student			D-2800 Lyngby,Denmark
> tel: +45 45 25 24 85		http://www.cbs.dtu.dk/laurent
>
> On Wed, 2 Jan 2002, Jean-Pierre Gattuso wrote:
>
> > Hi:
> >
> > I am a very recent user of R 1.40 for MacOSX and went through the
> > help files but did not not find the function that I am looking for.
> >
> >
> > I have two comma separated files which I want to partly combine.
> >
> > File 1 has 49670 lines and 26 columns (separated by commas) and looks
> > like this:
> >
> > ...
> > 86020,-10.25,30.25,19.24,19.18,6.19,22.6,16.41,.4,1.58,36.36,36.47,36.18,,3,3,4,116.44,3.03,98.83,126.97,116.03,5.44,0,0,-1
> > 86021,-9.75,30.25,19.06,18.97,6.24,22.41,16.17,.42,1.58,36.36,36.47,36.18,.01,3,3,4,146.15,5.33,131.7,153.38,145.6,9.46,0,0,-1
> > 86022,-9.25,30.25,,,,,,,,,,,,3,,,,,,,,,0,0,-1
> > 86023,-8.75,30.25,,,,,,,,,,,,,,,,,,,,,0,0,-1
> > 86071,15.25,30.25,,,,,,,,,,,,3,,,,,,,,,0,0,-1
> > ....
> >
> > File 2 has 259201 lines and 8 columns (separated by commas) and looks
> > like this:
> >
> > ...
> > 86020,Oceanic I,-1,-10.25,30.25,7,2670.172,207.249
> > 86021,Coastal,0,-9.75,30.25,7,2670.172,207.249
> > 86022,Terrestrial,1,-9.25,30.25,7,2670.172,207.249
> > 86023,Terrestrial,1,-8.75,30.25,7,2670.172,207.249
> > 86024,Not Selected,-9999,-8.25,30.25,7,2670.172,207.249
> > ...
> >
> > Column 1 in both files is the cell id and colum 2 in file 2 provides
> > the nature of the cell (oceanic, terrestrial...).
> >
> > What I want to do is to add the nature of the cell to each line of
> > file 1. In other words, I want to add column 2 of file 2 (e.g.
> > "oceanic") to file 1, using columns 1 of both files as matching
> > variables (i.e. add column 2 o
> >
> > Thanks in advance for your help,
> > jp
> > --
> >
> > ____________________________________________________________________
> > Jean-Pierre Gattuso
> > Laboratoire d'Océanographie de Villefranche, UMR 7093 CNRS-UPMC
> > B. P. 28, F-06234 Villefranche-sur-mer Cedex - France
> > Voice: +33 (0)493763859 - Fax: +33 (0)493763834 - US fax: +1 (978) 477-8302
> > <mailto:gattuso at obs-vlfr.fr> - http://www.obs-vlfr.fr/~gattuso
> > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> > r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> > Send "info", "help", or "[un]subscribe"
> > (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
> >
>
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list