Skip to content
Back to formatted view

Raw Message

Message-ID: <Pine.LNX.4.44.0306090851290.20965-100000@stat61.stat.auckland.ac.nz>
Date: 2003-06-08T20:54:02Z
From: Ko-Kang Kevin Wang
Subject: Basic question on applying a function to each row of a dataframe
In-Reply-To: <Law11-F101oZolmRBaY00009f20@hotmail.com>

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)