Spline on multiple factors in data frame

2019-08-15 05:17发布

问题:

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.

So I have the following 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)))

Give some names:

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

Which creates:

> 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

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.

Like:

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...

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.

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.

回答1:

What about this:

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


标签: r plyr spline