样条数据框架中的多个因素 [英] Spline on multiple factors in data frame

查看:69
本文介绍了样条数据框架中的多个因素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是在我有很多Model类型(每个类都相同)的情况下进行的,但是每个Model的数据量很小,我想通过样条获得更完整的数据集.我希望找到一种方法,而不必一次花键分别对每个模型进行花键.

This question is in the context where I have a lot Model types, each of the same class, but the amount of data for each Model is small and I want to spline to get a fuller dataset. I'm hoping to find a way to do this without having to individually spline every Model once at a time.

所以我有以下df:

mydf<- data.frame(c("a","a","b","b","c","c"),c("e","e","e","e","e","e")
                 ,as.numeric(c(1,2,3,10,20,30)),
                 as.numeric(c(5,10,20,20,15,10)))

给一些名字

colnames(mydf)<-c("Model", "Class","Seconds", "Speed")

哪个创建了

> mydf
  Model Class Seconds Speed
1     a     e       1     5
2     a     e       2    10
3     b     e       3    20
4     b     e      10    20
5     c     e      20    15
6     c     e      30    10

所以我想要每个模型的Seconds和Speed列上有一个样条.因此,例如,如果我在模型"a"上使用样条线,则只将这些样条在"a"上作为样条线即可.

So I want a spline on the Seconds and Speed columns for each Model. So for example if I used spline on Model "a", it you only spline those elements on "a" as the model.

赞:

spline(x=mydf[1:2,3], y=mydf[1:2,4])
$x
[1] 1.0 1.2 1.4 1.6 1.8 2.0

$y
[1]  5  6  7  8  9 10

这有效,但是当您有数百个模型时...

This works but when you have a hundreds of models...

我只想使用"a"来样条化"a",然后将其移动到"b"并只样条化"b"等.理想情况下,它将输出为新的数据帧,但是在这一点上,我想不出现错误.

I want to spline "a" only using "a" and then it moves to "b" and splines only "b" etc. Ideally it would output as a new dataframe but at this point I'd just like to not get an error.

我在plyr中尝试ddply,但出现错误.我希望避免使用循环或带有循环的函数,但是如果那是唯一的选择,那么...

I tried ddply in plyr but getting errors. I'm hoping to avoid using loops or functions with loops but if that's the only option then...

谢谢,请告诉我是否可以改善问题.

Thanks and please let me know if I can improve the question.

推荐答案

这是怎么回事:

ddply(mydf, .(Model), summarise, Spline = spline(x = Seconds, y = Speed), 
      Var = c("Seconds", "Speed"))
  Model                        Spline     Var
1     a  1.0, 1.2, 1.4, 1.6, 1.8, 2.0 Seconds
2     a             5, 6, 7, 8, 9, 10   Speed
3     b 3.0, 4.4, 5.8, 7.2, 8.6, 10.0 Seconds
4     b        20, 20, 20, 20, 20, 20   Speed
5     c        20, 22, 24, 26, 28, 30 Seconds
6     c        15, 14, 13, 12, 11, 10   Speed

这篇关于样条数据框架中的多个因素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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