[R] splitting a data.frame

David Meyer david.meyer at ci.tuwien.ac.at
Tue Jan 22 15:04:09 CET 2002


Gary Collins wrote:
> 
>  I have the following (simple!?) problem which I am unable to find a
>  relatively trivial solution to.
>  If I have a dataframe,
> 
>  A    1
>  A    7
>  B    4
>  B    5
>  C    3
>  D    3
>  D    2
>  E    5
>  F    5
>  F    6
> 
>  I would like to create a new data.frame in the form
> 
>  ID    pt1    pt2
>  A    1    7
>  B    4    5
>  C    3    NA
>  D    3    2
>  E    5    NA
>  F    5    6
> 
>  so that for each identifier, in this example, A...F I have a column for
>  each observation for each identifier... (with a maximum of 2 obs per
>  identifier, if only 1 obs exist then the second obs pt2 is set to NA)
>  This is so I can find the absolute differences between the obs for each
>  identifier, that is abs(pt1-pt2)
> 
>  ID  Diff
>  A    6
>  B    1
>  C    NA
>  D    1
>  E    NA
>  F    1
>  for which there may be another approach so as not to mess about creating
>  a new dataframe
>  Any ideas?
>  Gary

What about

by(y,x,function(x) x[2]-x[1])

if x is your factor and y are your values?

-d

> 
> __________________________________________________
> Gary S. Collins, PhD,
> Statistics Research Fellow,
> Quality of Life Unit,
> European Organisation for Research and Treatment of Cancer,
> EORTC Data Center,
> Avenue E. Mounier 83, bte. 11,
> B-1200 Brussels, Belgium.
> 
> Tel: +32 2 774 1 606
> Fax: +32 2 779 4 568
> http://www.eortc.be/home/qol/
> __________________________________________________
> 
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> 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
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

-- 
	Mag. David Meyer		Wiedner Hauptstrasse 8-10
Vienna University of Technology		A-1040 Vienna/AUSTRIA
       Department for			Tel.: (+431) 58801/10772
Statistics and Probability Theory	mail: david.meyer at ci.tuwien.ac.at
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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