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

peter leonard pfleonard at hotmail.com
Mon Jun 9 00:05:33 CEST 2003


Hi Keven,
This returns :

Error in FUN(newX[, i], ...) : Argument "y" is missing, with no default


E.g

>x<-c(1,2,3,4)
>w<-c(1,1,1,1)
>DF<-data.frame(x,w)
>foo <- function(x, y)x+y apply(DF, 1, foo)
Error in FUN(newX[, i], ...) : Argument "y" is missing, with no default

Regards
Peter





>From: Ko-Kang Kevin Wang <kwan022 at stat.auckland.ac.nz>
>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: Mon, 9 Jun 2003 08:54:02 +1200 (NZST)
>
>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