R - ddply to act on 2 columns in 3 column data.frame -


i trying use ddply act on 2 columns in 3 column data.frame. know i've done before, life of me cannot work. here's example:

func = function(x, y) {   if(x>y) {     x-y   } else {     0   } } df = data.frame(name=c('w','x','y','z'), a=c(1,2,3,4), b=c(4,3,2,1)) 

here i've tried, along many other things:

ddply(df, summarize, func(a, b)) ddply(df, mutate, func(df$a, df$b)) ddply(df, func) 

the common error is:

error in usemethod("as.quoted") :    no applicable method 'as.quoted' applied object of class "function" 

expected output:

  name b result 1    w 1 4 0 2    x 2 3 0 3    y 3 2 1 4    z 4 1 3 

ddply(df,.(name), summarize, result=ifelse(a>b,a-b,0)) 

or

func = function(x, y) { ifelse(x>y,x-y,0)}  ddply(df,.(name), summarize, result=func(a,b)) 

run code in rfiddle


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -