[R] How to create data frame from data with unequal length

Peter Dalgaard P.Dalgaard at biostat.ku.dk
Wed Nov 28 19:38:40 CET 2007


tom soyer wrote:
> Hi,
>
> I have two sets of data that I would like to put into a data frame. But
> since they have different length, I am not sure how to do this. Here is an
> example of my data:
>
> data set one:
> date         growth
> 1/1/2007   10
> 1/2/2007   10.2
> 1/3/2007   10.4
> 1/4/2007   10.6
>
> data set two:
>  date         growth
> 1/1/2007   22
> 1/2/2007   22.5
> 1/4/2007   22.4
>
> I would like to combine the two data sets and create a data frame like this:
>  date         growthA    growthB
> 1/1/2007   10            22
> 1/2/2007   10.2         22.5
> 1/3/2007   10.4         NA
> 1/4/2007   10.6         22.4
>
> Or skipping the missing data point all together, like this:
>  date         growthA    growthB
> 1/1/2007   10            22
> 1/2/2007   10.2         22.5
> 1/4/2007   10.6         22.4
>
> Right now I am doing this by hand, and it is really time consuming. I am
> wondering if there is an easier way of creating data frames from unequal
> length data using existing R functions. Is there a way to create data
> with equal length based on the date column? I would appreciate any help from
> the group.
>
> Thanks,
>
>   
I'd have a look at merge() if I were you.

-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907



More information about the R-help mailing list