TBP
TBP

Reputation: 747

Using approx in ddply?

I am trying to use approx() to predict points on curves inside of ddply, but it does not seem to be working as I expect it to once it is handed to ddply.

This all works:

#Fake Data, V3 is my index variable
df<-data.frame(V1=rep(0:10,3), V2=c(exp(0:10), 2*exp(0:10), 3*exp(0:10)), V3=rep(1:3,each=11))

approxy<-function(i){
  estim<-approx(x=i$V1, y=i$V2, xout=c(1.1,5.1,9.1))$y
  return(data.frame(ex1=estim[1], ex5=estim[2], ex9=estim[3]))
}

approxy(df[df$V3==1,])

This does not:

ddply(df, c("V3"), fun=approxy)

It just spits the original dataframe back out. Any thoughts on this problem would be appreciated.

Upvotes: 2

Views: 106

Answers (1)

HubertL
HubertL

Reputation: 19544

Your syntax is incorrect:

ddply(df, c("V3"), .fun=approxy)

gives

  V3      ex1      ex5       ex9
1  1 3.185359 173.9147  9495.422
2  2 6.370719 347.8294 18990.844
3  3 9.556078 521.7442 28486.266

Upvotes: 4

Related Questions