interpolation of grouped data using data.table

允我心安 提交于 2019-12-07 07:39:11

问题


This is a continuation of a question that I had originally posted at http://r.789695.n4.nabble.com/subset-between-data-table-list-and-single-data-table-object-tp4673202.html . Matthew had suggested that I post my question here so I am doing that now.

This is my input below:

library(data.table)
library(pracma) # for the 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 am using this code to generate the interpolated tempsbigdata$y using the aimsmall$mean values by the site_no:

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

This is the output from the function meanpre, but it is 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 is what I would like to get:

meanpre
    site_no     V1  
1: 02446500     9.99
2: 02467000     75.66

Any suggestions? Thank you.

UPDATE 1:

Hugh, I used the approx function in the past and it is not accurate for my data; however, the interp1 function in pracma is accurate. The mean and p50 columns in aimsmall & the x values in tempsbigdata are discharge values. The y in tempsbigdata represent gage heights. I am using the interp1 function to determine the appropriate gage height or y value for the discharge values or mean (and p50).

Frank, thank you for your advice and suggested code. This is the output for your 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 I run the following code I get the result below:

 interp1(tempsbigdata$x, tempsbigdata$y, xi = aimsmall$mean, method ="linear")


 [1] 12.07599  9.99410 75.66424

Is there any way to get this in return? Thank you.

    site_no     V1  
1: 02446500     9.99
2: 02467000     75.66

UPDATE 2

Frank, thank you and I have added the code to make it easier to have the data in R. Pracma is an R package of numerical method routines that were ported from GNU Octave [similar to MATLAB(R)] to R. The interp1 function is a one-dimensional interpolation function.

Frank, that was perfect (your last comment about the R code for "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

来源:https://stackoverflow.com/questions/18165373/interpolation-of-grouped-data-using-data-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!