Hi, I have this code here and try to use sapply code. But I got error message that I don't really understand to correct. bt <- c(24.96874, 19.67861, 23.51001, 19.86868); round(bt,2) alp <- c(2.724234, 3.914649, 3.229146, 3.120719); round(alp,2) bt_alp <- data.frame(bt,alp) sapply(bt_alp, function(bt,alp) ((bt_m/bt)^alp), bt_m = min(bt))> sapply(bt_alp, function(bt,alp) ((bt_m/bt)^alp), bt_m = min(bt))Error in FUN(X[[1L]], ...) : unused argument(s) (bt_m = 19.67861) Thank you for any help given. [[alternative HTML version deleted]]
Roslina Zakaria wrote:> Hi, > > I have this code here and try to use sapply code. But I got error message that I don't really understand to correct. > > bt <- c(24.96874, 19.67861, 23.51001, 19.86868); round(bt,2) > alp <- c(2.724234, 3.914649, 3.229146, 3.120719); round(alp,2) > > bt_alp <- data.frame(bt,alp) > > sapply(bt_alp, function(bt,alp) ((bt_m/bt)^alp), bt_m = min(bt)) > > >> sapply(bt_alp, function(bt,alp) ((bt_m/bt)^alp), bt_m = min(bt)) >> > Error in FUN(X[[1L]], ...) : unused argument(s) (bt_m = 19.67861) > > > Thank you for any help given. >I am not clear to what you are trying to do. Perhaps... bt <- c(24.96874, 19.67861, 23.51001, 19.86868); round(bt,2) alp <- c(2.724234, 3.914649, 3.229146, 3.120719); round(alp,2) bt_m <- min(bt) x <- (bt_m/bt)^alp sapply is not normally applied to data frames, because data frames are lists of columns, and most people don't want to apply the same algorithm to a bunch of columns. If you did process one row at a time with sapply, you would no longer be able to "see" enough data to compute min(bt) inside the anonymous function.> ------------------------------------------------------------------------ > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
Hi Roslina, Try the following instead: # option 1 transform(bt_alp, result = (min(bt)/bt)^alp) # option 2 with(bt_alp, (min(bt)/bt)^alp) HTH, Jorge On Wed, Jun 16, 2010 at 11:17 PM, Roslina Zakaria <> wrote:> Hi, > > I have this code here and try to use sapply code. But I got error message > that I don't really understand to correct. > > bt <- c(24.96874, 19.67861, 23.51001, 19.86868); round(bt,2) > alp <- c(2.724234, 3.914649, 3.229146, 3.120719); round(alp,2) > > bt_alp <- data.frame(bt,alp) > > sapply(bt_alp, function(bt,alp) ((bt_m/bt)^alp), bt_m = min(bt)) > > > sapply(bt_alp, function(bt,alp) ((bt_m/bt)^alp), bt_m = min(bt)) > Error in FUN(X[[1L]], ...) : unused argument(s) (bt_m = 19.67861) > > > Thank you for any help given. > > > > [[alternative HTML version deleted]] > > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >[[alternative HTML version deleted]]
I believe you have received advice on how to solve your problem already, this is just to say why your 'sapply' call failed. Your call to 'sapply' assumes that there is a 'bt_m' argument to the function, which there isn't. Another problem might be that there is an 'alp' argument but that is not given in the 'sapply' call. On 17/06/2010 04:17, Roslina Zakaria wrote:> Hi, > > I have this code here and try to use sapply code. But I got error message that I don't really understand to correct. > > bt<- c(24.96874, 19.67861, 23.51001, 19.86868); round(bt,2) > alp<- c(2.724234, 3.914649, 3.229146, 3.120719); round(alp,2) > > bt_alp<- data.frame(bt,alp) > > sapply(bt_alp, function(bt,alp) ((bt_m/bt)^alp), bt_m = min(bt)) > >> sapply(bt_alp, function(bt,alp) ((bt_m/bt)^alp), bt_m = min(bt)) > Error in FUN(X[[1L]], ...) : unused argument(s) (bt_m = 19.67861) > > > Thank you for any help given. > > > > [[alternative HTML version deleted]] > > > > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- Patrick Burns pburns at pburns.seanet.com http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno')