[R] help with simple but massive data transformation

David Winsemius dwinsemius at comcast.net
Mon Oct 11 16:51:13 CEST 2010


On Oct 11, 2010, at 10:16 AM, clee wrote:

>
> I have data that looks like this:
>
> start     end     value
> 1          4         2
> 5          8         1
> 9         10        0
>
>
> I want to transform the data so that it becomes:
>
> startend     value
> 1               2
> 2               2
> 3               2
> 4               2
> 5               1
> 6               1
> 7               1
> 8               1
> 9               0
> 10             0

 > do.call("rbind",
            apply(dta, 1,
              function(.r) matrix(c(
                           seq(.r[1], .r[2]),
                           vals=rep(.r[3], .r[2]-.r[1]+1) ),
                           ncol=2) ))
       [,1] [,2]
  [1,]    1    2
  [2,]    2    2
  [3,]    3    2
  [4,]    4    2
  [5,]    5    1
  [6,]    6    1
  [7,]    7    1
  [8,]    8    1
  [9,]    9    0
[10,]   10    0

>
> ----
> I've written a for loop that can do the transformation BUT I need to  
> do this
> on very large datasets (millions of rows).  Does anyone know of an R  
> package
> that has a function that can do this transformation?
>
> Any help is much appreciated!
>
> Thanks!
> -- 
> View this message in context: http://r.789695.n4.nabble.com/help-with-simple-but-massive-data-transformation-tp2989850p2989850.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list