r - interpolation of grouped data using data.table -


this continuation of question had posted @ http://r.789695.n4.nabble.com/subset-between-data-table-list-and-single-data-table-object-tp4673202.html . matthew had suggested post question here doing now.

this input below:

library(data.table) library(pracma) # interp1 function    tempbigdata1 <- data.table(c(14.80, 14.81, 14.82), c(7900, 7920, 7930),   c("02437100", "02437100", "02437100"))   tempbigdata2 <- data.table(c(9.98, 9.99, 10.00), c(816, 819, 821),  c("02446500", "02446500", "02446500"))   tempbigdata3 <- data.table(c(75.65, 75.66, 75.67), c(23600, 23700, 23800), c("02467000", "02467000", "02467000"))    tempsbigdata <- rbind(tempbigdata1, tempbigdata2, tempbigdata3)    setnames(tempsbigdata,c("y", "x", "site_no"))    setkey(tempsbigdata, site_no)  tempsbigdata    y        x       site_no 1: 14.80    7900        02437100 2: 14.81    7920        02437100 3: 14.82    7930        02437100 4:  9.98    816         02446500 5:  9.99    819         02446500 6: 10.00    821         02446500 7: 75.65    23600       02467000 8: 75.66    23700       02467000 9: 75.67    23800       02467000    aimsmall <- data.table(c("02437100", "02446500", "02467000"), c(3882.65, 819.82, 23742.37), c(1830.0, 382.0, 10400.0))   setnames(aimsmall,c("site_no", "mean", "p50"))   setkey(aimsmall, site_no)  aimsmall     site_no     mean    p50 1: 02437100     3882.65     1830 2: 02446500     819.82      382 3: 02467000     23742.37    10400 

i using code generate interpolated tempsbigdata$y using aimsmall$mean values site_no:

  meanpre <- tempsbigdata[,if(aimsmall$mean > min(tempsbigdata$x){              interp1(tempsbigdata$x, tempsbigdata$y,                   xi = aimsmall$mean, method ="linear")},by=site_no] 

this output function meanpre, not correct.

meanpre     site_no        v1 1: 02437100     12.07599 2: 02437100     9.99410 3: 02437100     19.56813 4: 02446500     12.07599 5: 02446500     9.99410 6: 02446500     19.56813 7: 02467000     12.07599 8: 02467000     9.99410 9: 02467000     19.56813 

this get:

meanpre     site_no     v1   1: 02446500     9.99 2: 02467000     75.66 

any suggestions? thank you.

update 1:

hugh, used approx function in past , not accurate data; however, interp1 function in pracma accurate. mean , p50 columns in aimsmall & x values in tempsbigdata discharge values. y in tempsbigdata represent gage heights. using interp1 function determine appropriate gage height or y value discharge values or mean (and p50).

frank, thank advice , suggested code. output suggested code:

tempsbigdata[aimsmall][,if(mean[1] > min(x)){interp1(tempsbigdata$x,tempsbigdata$y, xi = aimsmall$mean, method ="linear")},by=site_no]   site_no       v1 1: 02446500 12.07599 2: 02446500  9.99410 3: 02446500 75.66424 4: 02467000 12.07599 5: 02467000  9.99410  6: 02467000 75.66424 

when run following code result below:

 interp1(tempsbigdata$x, tempsbigdata$y, xi = aimsmall$mean, method ="linear")    [1] 12.07599  9.99410 75.66424 

is there way in return? thank you.

    site_no     v1   1: 02446500     9.99 2: 02467000     75.66 

update 2

frank, thank , have added code make easier have data in r. pracma r package of numerical method routines ported gnu octave [similar matlab(r)] r. interp1 function one-dimensional interpolation function.

frank, perfect (your last comment r code "do stuff"):

 tempsbigdata[aimsmall][,if(mean[1] > min(x)){interp1(x, y, xi = mean[1], method ="linear")},by=site_no]   site_no       v1  1: 02446500  9.99410  2: 02467000 75.66424 


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -