在R中对分组数据使用近似函数

时间:2015-09-11 01:10:47

标签: r interpolation

我有一个包含Id,Vg,Device,Die,W,L等列的大数据集(与此问题无关)。我想在给定的Id值下插入Vg,但是必须对按Device和Die列分组的数据执行此操作。

我的示例数据看起来像

Die     Device      Id      Vg     W   L 
  1    Device1       1       0    10   1  
  1    Device1     1.2     0.1    10   1  
  1    Device1     1.3     0.2    10   1
  1    Device2       1       0    10   2
  1    Device2     1.2     0.1    10   2  
  1    Device2     1.3     0.2    10   2
  1    Device3       1       0    10   3
  1    Device3     1.2     0.1    10   3  
  1    Device3     1.3     0.2    10   3

每个裸片有22个独特的器件。每个模具上有67个模具和22个设备名称是相同的。因此,如果我为Id = 1.25插入Vg,我希望得到22 * 67的Vg值,Id = 1.25。

以下是我正在尝试的代码

data_tidy%>%
  group_by(Die,Device)%>% #Die is numeric, Device is factor
  mutate(Vt=approx(x=log10(Id),y=Vg,xout=log10(3e-8*W/L))$y)

这类似于建议的here,我正在从下面的链接复制建议的代码

df %>%
  group_by(variable) %>%
  arrange(variable, event.date) %>%
  mutate(time=seq(1,n())) %>%
  mutate(ip.value=approx(time,value,time)$y) %>%
  select(-time)

但是,当我执行上面的代码时,我收到一条错误消息

  

错误:无法复制大小为18的载体

1 个答案:

答案 0 :(得分:1)

这是一个data.table解决方案:

library(data.table)
f <- function(x) setDT(df)[,approx(Id,Vg,x), by=list(Device,Die)]
f(1.25)
#     Device Die    x    y
# 1: Device1   1 1.25 0.15
# 2: Device2   1 1.25 0.15
# 3: Device3   1 1.25 0.15

此处列y是插值。