[R] help with matrix creation (merging data frames)

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Aug 10 17:01:44 CEST 2000


> Date: Thu, 10 Aug 2000 15:33:39 -0700
> From: Jesus Maria Frias Celayeta <iosu at bureau.ucc.ie>
> 
> hi R-help!
> 
>     I would like to know a simple and easy way (if posible) to do the 
following
> data manipulation. I have a matrix of experimental data (with replicae in 
time)

actually, they seem to be data frames.

> 
> 
>experiment<-data.frame(times=c(0,0,10,10,20,20,30,30),expval=c(1,1,2,2,3,3,4,4)
)
> 
> > experiment
>   times expval
> 1     0      1
> 2     0      1
> 3    10      2
> 4    10      2
> 5    20      3
> 6    20      3
> 7    30      4
> 8    30      4
> 
> and a set of simulated values
> 
> > simul<-data.frame(times=c(0,10,20,30),simul=c(3,4,5,6))
> > simul
>   times simul
> 1     0     3
> 2    10     4
> 3    20     5
> 4    30     6
> 
> >From this simulated data, I would like to obtain a data frame like this
> 
>   times expval simul
> 1     0      1     3
> 2     0      1     3
> 3    10      2     4
> 4    10      2     4
> 5    20      3     5
> 6    20      3     5
> 7    30      4     6
> 8    30      4     6
> 
> expanding each of the simulated values as many times as duplicates 
(triplicates
> or anything) I have in the data. I have come across duplicate and match, but I
> don't see a way forward from here, unless I make a for loop and I match row by
> row the time vectors. I am sure that is a very easy thing, and I apologize in
> advance for the stupid question, but I am kind of stuck.

> merge(experiment, simul)
  times expval simul
1     0      1     3
2     0      1     3
3    10      2     4
4    10      2     4
5    20      3     5
6    20      3     5
7    30      4     6
8    30      4     6

does all the work for you.

-- 
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