allardvinc
allardvinc

Reputation: 69

applying a function within ddply

I'm tryin to apply a group-wise function using ddply. My data looks like that:

https://echange-fichiers.inra.fr/get?k=b1jD63CWkT93hDsbZ0g

The following function calculates x value for y=0

intercept=function (x,y){

    if (length(y[y==0])==0){
      x1=min(x[y >0])
      y1=min(y[y >0])

      x2=max(x[y <0])
      y2=max(y[y <0])

      p =(y2-y1)/(x2-x1)

      x0= -y1/p + x1

    }else{
      x0=x[y==0]

    }
return(x0)
}

then I try to apply this by group with ddply:

zzz=ddply(Data,.(Genotype,T), summarise
         InterDays=(intercept(Data$Days,Data$Diff)) 
)

My grammar must be wrong since it does no not return a output with a similar format than :

zzz=ddply(DataAll,.(Genotype,T), summarise
         InterDays=mean(DataAll$Days) 
)

I think there is something wrong with the way I call my function variables but I don't see where... Any help welcome. All the best, Vincent

Upvotes: 2

Views: 856

Answers (1)

juba
juba

Reputation: 49073

I think you should call ddply this way :

ddply(Data,.(Genotype,T), summarise,
         InterDays=(intercept(Days,Diff)) 
)

IIUC, if you call your intercept function with Data$Days and Data$Diff as arguments, the whole vectors will be passed to the function each time. With Days and Diff, only the values corresponding to the current subset of Data is used.

Upvotes: 2

Related Questions