Friday, 13 September 2013

Using a function within ddply

Using a function within ddply

I'm having an issue writing functions into a ddply call. Here is a basic DF:
Time<-c(1,2,3,4,5,6,7,8,9,10)
X<-c(1,2,-3,4,-7,2,-4,9,7,-5)
Y<-c(2,-3,-4,4,4,3,2,-9,9,10)
T<-c(5,4,3,2,1,9,8,7,6,5)
DF<-data.frame(Time,X,Y,T)
I have two different functions. One that looks to subtract a number from T
depending on the results of a two column statement
VD<-function(X,Y,T){
if(X > 0 & Y < 0) {9-T}
if(X < 0 & Y < 0) {5-T}
if(X > 0 & Y > 0) {9-T}
if(X < 0 & Y > 0) {5-T}
}
and a second that just looks at whether a number is negative or positive
(subtract x from 9 if negative number, do nothing if positive number)
VD2<-function(X){
if(X<0) {9-X}
}
I wrote what I thought would work
ddply(DF,'Time',summarize,Result=VD(X,Y,T))
ddply(DF,'Time',summarize,Result2=VD2(X))
But I get errors for both, and I'm not sure why

No comments:

Post a Comment