Skip to content

Subtracting elements of data.frame

6 messages · Vincy Pyne, Dennis Murphy, Ivan Calandra +2 more

#
Hi,

Try this:
df_new <- as.data.frame(lapply(df, FUN=function(x) x-x[1]))

I hope it works!
Ivan

Le 1/25/2011 10:20, Vincy Pyne a ?crit :

  
    
#
I always forget about sapply():
df_new <- sapply(df, FUN=function(x) x-x[1])

Ivan

Le 1/25/2011 10:33, Ivan Calandra a ?crit :

  
    
#
On 2011-01-25 01:20, Vincy Pyne wrote:
You've already had good simple solutions. Here's one more,
using the plyr package:

  require(plyr)
  df_new <- colwise(.fun = function(x) {x - x[1]})(df)

plyr is very handy for all sorts of data manipulations.

Peter Ehlers