This question is similar to Expand Data Frame, but my issue is more complex. I have a data set, which I converted to a data table "dt" as in the example mentioned.
> dt
site year plot ht count
1: 0001 00 1 0.0833 3
2: 0001 00 2 0.2 5
3: 0001 02 2 0.75 1
4: 0001 02 2 0.0833 3
5: 0002 00 1 0.0833 1
----
#Truncated (it's got over 200,000 rows)
> str(dt)
Classes ‘data.table’ and 'data.frame': 220116 obs. of 5 variables:
$ site : chr "0001" "0001" "0001" "0001" ...
$ year : chr "00" "00" "02" "02" ...
$ plot : int 1 2 2 2 1 1 2 2 3 ...
$ ht : num 0.0833 0.0833 0.75 0.0833 0.0833 5.5 0.0833 0.0833 0.0833
0.0833 ...
$ count: num 3 15 1 3 1 3 1 1 1 2 ...
- attr(*, ".internal.selfref")=<externalptr>
>
I'd like to expand the data set so that every height has its own row. The number of rows to be determined by the count column. It should look something like this:
site year plot ht
0001 00 1 0.0833
0001 00 1 0.0833
0001 00 1 0.0833
0001 00 2 0.2
0001 00 2 0.2
0001 00 2 0.2
0001 00 2 0.2
0001 00 2 0.2
0001 02 2 0.75
0001 02 2 0.0833
----
I've attempted using something similar to the function in the Expand Data Frame example:
f<-function(x,y,len=max(y)) {res<-numeric(len);res[y]<-x;res}
dt_expd<-dt[,list(ht=f(ht,count,count)),by=c(site,year,plot)]
And I get this error:
Error in eval(expr, envir, enclos) : object 'site' not found
The challenges are:
Expanding rows with 'ht' that retain the correct site, year, and plot #
Old university computer
This is a small part for my graduate thesis. Any help is greatly appreciated!
-Lake Graboski