Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to reuse generated columns in ddply?

Tags:

r

plyr

I have a script where I'm using ddply, as in the following example:

ddply(df, .(col),
function(x) data.frame(
col1=some_function(x$y),
col2=some_other_function(x$y)
)
)

Within ddply, is it possible to reuse col1 without calling the entire function again?

For example:

ddply(df, .(col),
function(x) data.frame(
col1=some_function(x$y),
col2=some_other_function(x$y)
col3=col1*col2
)
)
like image 260
Brandon Bertelsen Avatar asked Dec 29 '22 09:12

Brandon Bertelsen


1 Answers

You've got a whole function to play with! Doesn't have to be a one-liner! This should work:

ddply(df, .(col), function(x) {
  tmp <- some_other_function(x$y)
  data.frame(
    col1=some_function(x$y),
    col2=tmp,
    col3=tmp
  )
})
like image 113
Harlan Avatar answered Dec 31 '22 02:12

Harlan