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

Ko-Kang Kevin Wang kwan022 at stat.auckland.ac.nz
Sun Jun 8 22:54:02 CEST 2003


Hi,

You need to tell the apply() whether you want to apply the function to 
rows (1) or columns (2).

So in your case you may want to try something like:
  apply(DF, 1, foo)

On Sun, 8 Jun 2003, peter leonard wrote:

> 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)
> 

-- 
Cheers,

Kevin

------------------------------------------------------------------------------
"On two occasions, I have been asked [by members of Parliament],
'Pray, Mr. Babbage, if you put into the machine wrong figures, will
the right answers come out?' I am not able to rightly apprehend the
kind of confusion of ideas that could provoke such a question."

-- Charles Babbage (1791-1871) 
---- From Computer Stupidities: http://rinkworks.com/stupid/

--
Ko-Kang Kevin Wang
Master of Science (MSc) Student
SLC Tutor and Lab Demonstrator
Department of Statistics
University of Auckland
New Zealand
Homepage: http://www.stat.auckland.ac.nz/~kwan022
Ph: 373-7599
    x88475 (City)
    x88480 (Tamaki)




More information about the R-help mailing list