[R] Basic question on applying a function to each row of a dataframe

peter leonard pfleonard at hotmail.com
Mon Jun 9 06:21:01 CEST 2003


This works fine.
Thanks
Peter


>From: Spencer Graves <spencer.graves at PDF.COM>
>To: peter leonard <pfleonard at hotmail.com>
>CC: r-help at stat.math.ethz.ch
>Subject: Re: [R] Basic question on applying a function to each row of 
>a	dataframe
>Date: Sun, 08 Jun 2003 13:48:04 -0700
>
>How about the following:
>
> > DF <- data.frame(x=1:4, y=rep(1,4))
> > foo <- function(x, y)x+y
> > foo(DF$x, DF$y)
>[1] 2 3 4 5
>
>hth.  spencer graves
>
>peter leonard wrote:
>>Hi,
>>
>>I have a function foo(x,y) and a dataframe, DF,  comprised of two vectors, 
>>x & w,  as follows :
>>
>>   x w
>>1  1 1
>>2  2 1
>>3  3 1
>>4  4 1
>>
>>etc
>>
>>
>>I would like to apply the function foo to each 'pair' within DF e.g  
>>foo(1,1), foo(2,1), foo(3,1) etc
>>
>>I have tried
>>
>>>apply(DF,foo)
>>>apply(DF[,],foo)
>>>apply(DF[DF$x,DF$w],foo)
>>
>>
>>
>>However, none of the above worked. Can anyone help ?
>>
>>Thanks in advance,
>>Peter
>>
>>______________________________________________
>>R-help at stat.math.ethz.ch mailing list
>>https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>
>




More information about the R-help mailing list