peter leonard
2003-Jun-08 20:35 UTC
[R] Basic question on applying a function to each row of a dataframe
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
Spencer Graves
2003-Jun-08 20:48 UTC
[R] Basic question on applying a function to each row of a dataframe
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
Ko-Kang Kevin Wang
2003-Jun-08 20:54 UTC
[R] Basic question on applying a function to each row of a dataframe
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)
peter leonard
2003-Jun-08 22:05 UTC
[R] Basic question on applying a function to each row of a dataframe
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) > >
peter leonard
2003-Jun-09 04:21 UTC
[R] Basic question on applying a function to each row of a dataframe
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 > >
Thomas Lumley
2003-Jun-09 13:47 UTC
[R] Basic question on applying a function to each row of a dataframe
On Sun, 8 Jun 2003, 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 >In R 1.7.0 there is a function mapply() for this sort of thing. mapply("foo",DF[,1],DF[,2]) -thomas
peter leonard
2003-Jun-10 00:05 UTC
[R] Basic question on applying a function to each row of a dataframe
This works fine. Thanks everybody for all your help, Peter>From: Thomas Lumley <tlumley at u.washington.edu> >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 06:47:20 -0700 (PDT) > >On Sun, 8 Jun 2003, 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 > > > >In R 1.7.0 there is a function mapply() for this sort of thing. > >mapply("foo",DF[,1],DF[,2]) > > -thomas >