对R中的分组数据使用近似函数 [英] Using approx function on grouped data in R

查看:54
本文介绍了对R中的分组数据使用近似函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含ID,Vg,Device,Die,W,L和其他列的大数据集(与该问题无关).我想以给定的Id值对Vg进行插值,但是必须对按Device and Die列分组的数据执行此操作.

I have a big data set with columns Id, Vg, Device, Die, W ,L and others (not relevant to this question). I want to interpolate Vg at a given value of Id but this operation has to be performed on data grouped by column Device and 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进行插值,那么我期望对Id = 1.25的Vg取22 * 67的值.

Each die has 22 unique devices. There are 67 dies and 22 Device names on each die are the same. Therefore if I interpolate Vg for Id=1.25, I expect to get 22*67 values of Vg for 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)

这类似于建议的此处,我正在从下方链接

This is similar to what is suggested here and I am copying the suggested code from the link below

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

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

However, when I execute my code above I get an error message saying

错误:无法复制大小为18的向量

Error: impossible to replicate vector of size 18

推荐答案

以下是data.table解决方案:

Here's a data.table solution:

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 列是插值.

这篇关于对R中的分组数据使用近似函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆