sent this to the list yesterday but didn't see it listed in the daily summary ... apologies if you receive it twice ... ________________________________ From: Afshartous, David Sent: Tuesday, June 27, 2006 10:02 AM To: 'r-help@stat.math.ethz.ch' Subject: sapply question All: I'm trying to use sapply to break up data within another function. (tapply doens't seem to work since I want to access several variables of a data set, not just break up a single variable according to a factor.) I tried the R documentation but that wasn't much help on this. The first function in the attached file works, breaking up the data according to patient number (6 observations per 24 patients) via a for loop i=1,..24. The second function is an attempt to use sapply instead of the first "for loop". it doesn't load properly and I seem to be absolutely blind as to why; my guess is that it's something "obvious". any help appreciated. (necessary data attached) thanks! dave ps: I'm on windows and, r.2 =100 (input to function) dat = comp.CAND.frm (attached file) [[alternative HTML version deleted]]
If you attached files, none made it through the mailing list. Can't you try posting a simplified version of the problem instead? Try to make it as easy for others to help you as possible will maximize the chance you get some useful response. Asking people to read code you've attached in files isn't helping, especially if the code is long and make use of data not accessible to anyone but you. In general, if you want something like tapply, but need several variables instead of one, you can use something like: sapply(split(dataFrame, factor_to_split_by), function(...)) HTH, Andy From: Afshartous, David> > sent this to the list yesterday but didn't see it listed in > the daily summary ... apologies if you receive it twice ... > > ________________________________ > > From: Afshartous, David > Sent: Tuesday, June 27, 2006 10:02 AM > To: 'r-help at stat.math.ethz.ch' > Subject: sapply question > > > All: > > I'm trying to use sapply to break up data within another function. > (tapply doens't seem to work since I want to access several > variables of > a > data set, not just break up a single variable according to a factor.) > I tried the R documentation but that wasn't much help on this. > > The first function in the attached file works, breaking up the data > according to patient number (6 > observations per 24 patients) via a for loop i=1,..24. > > The second function is an attempt to use sapply instead of the first > "for loop". > it doesn't load properly and I seem to be absolutely blind as > to why; my > guess is that > it's something "obvious". any help appreciated. (necessary data > attached) > > thanks! > dave > > ps: > I'm on windows and, > r.2 =100 (input to function) > dat = comp.CAND.frm (attached file) > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >
Hi from posting guide Technical details of posting: See General Instructions for more details of the following: No binary ***attachments*** except for PS, PDF, and some image and archive formats (others are automatically stripped off because they can contain malicious software). Files in other formats and larger ones should rather be put on the web and have only their URLs posted. This way a reader has the option to download them or not. So your attachement was stripped off. Tapply uses only one variable but you can use ?by or ?aggregate. by gives you a list, aggregate a data frame by(part.of.your.data.frame[,c(1,5,8:11,15)], your.column, function) aggregate(part.of.your.data.frame[,c(1,5,8:11,15)], list(your.columns, function) HTH Petr On 28 Jun 2006 at 10:06, Afshartous, David wrote: Date sent: Wed, 28 Jun 2006 10:06:34 -0400 From: "Afshartous, David" <afshart at exchange.sba.miami.edu> To: "Afshartous, David" <afshart at exchange.sba.miami.edu>, <r-help at stat.math.ethz.ch> Subject: [R] sapply question> sent this to the list yesterday but didn't see it listed in the daily > summary ... apologies if you receive it twice ... > > ________________________________ > > From: Afshartous, David > Sent: Tuesday, June 27, 2006 10:02 AM > To: 'r-help at stat.math.ethz.ch' > Subject: sapply question > > > All: > > I'm trying to use sapply to break up data within another function. > (tapply doens't seem to work since I want to access several variables > of a data set, not just break up a single variable according to a > factor.) I tried the R documentation but that wasn't much help on > this. > > The first function in the attached file works, breaking up the data > according to patient number (6 observations per 24 patients) via a for > loop i=1,..24. > > The second function is an attempt to use sapply instead of the first > "for loop". it doesn't load properly and I seem to be absolutely > blind as to why; my guess is that it's something "obvious". any help > appreciated. (necessary data attached) > > thanks! > dave > > ps: > I'm on windows and, > r.2 =100 (input to function) > dat = comp.CAND.frm (attached file) > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.htmlPetr Pikal petr.pikal at precheza.cz
Andy, My apologies for my very poor email. Below is some simplified code. I think my confusion has more to do w/ embedding the sapplpy within another function and returning the result, thus this would apply to your "split" suggestion as well. However, when I run this the result is still empty. thanks! dave creatine.function.new.3 = function(dat) { result = sapply(levels(dat$Patient_no), function(i) { subdat <- subset(dat, dat$Patient_no==i) U = subdat$UV r = numeric(6) for (i in 1:6) { r[i] = 2*U[i] } r ## results for each patient } ) result } -----Original Message----- From: Liaw, Andy [mailto:andy_liaw at merck.com] Sent: Wednesday, June 28, 2006 10:23 AM To: Afshartous, David; r-help at stat.math.ethz.ch Subject: RE: [R] sapply question If you attached files, none made it through the mailing list. Can't you try posting a simplified version of the problem instead? Try to make it as easy for others to help you as possible will maximize the chance you get some useful response. Asking people to read code you've attached in files isn't helping, especially if the code is long and make use of data not accessible to anyone but you. In general, if you want something like tapply, but need several variables instead of one, you can use something like: sapply(split(dataFrame, factor_to_split_by), function(...)) HTH, Andy From: Afshartous, David> > sent this to the list yesterday but didn't see it listed in the daily > summary ... apologies if you receive it twice ... > > ________________________________ > > From: Afshartous, David > Sent: Tuesday, June 27, 2006 10:02 AM > To: 'r-help at stat.math.ethz.ch' > Subject: sapply question > > > All: > > I'm trying to use sapply to break up data within another function. > (tapply doens't seem to work since I want to access several variables > of a data set, not just break up a single variable according to a > factor.) I tried the R documentation but that wasn't much help on > this. > > The first function in the attached file works, breaking up the data > according to patient number (6 observations per 24 patients) via a for> loop i=1,..24. > > The second function is an attempt to use sapply instead of the first > "for loop". > it doesn't load properly and I seem to be absolutely blind as to why; > my guess is that it's something "obvious". any help appreciated. > (necessary data > attached) > > thanks! > dave > > ps: > I'm on windows and, > r.2 =100 (input to function) > dat = comp.CAND.frm (attached file) > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >------------------------------------------------------------------------ ------ Notice: This e-mail message, together with any attachments,...{{dropped}}
Here's an example, using a data frame with two variables plus a grouping factor:> (dat <- data.frame(x1=1:10, x2=10:1, g=gl(2, 5)))x1 x2 g 1 1 10 1 2 2 9 1 3 3 8 1 4 4 7 1 5 5 6 1 6 6 5 2 7 7 4 2 8 8 3 2 9 9 2 2 10 10 1 2 Now, I want to compute 2 * x1 + x2 for each group in g:> sapply(split(dat, dat$g), function(d) 2 * d$x1 + d$x2)1 2 [1,] 12 17 [2,] 13 18 [3,] 14 19 [4,] 15 20 [5,] 16 21 Does that help? Andy From: Afshartous, David> > Andy, > My apologies for my very poor email. Below is some simplified code. > I think my confusion has more to do w/ embedding the sapplpy > within another function and returning the result, thus this > would apply to your "split" suggestion as well. > However, when I run this the result is still empty. > thanks! > dave > > > creatine.function.new.3 = function(dat) { > result = sapply(levels(dat$Patient_no), function(i) > { > subdat <- subset(dat, dat$Patient_no==i) > U = subdat$UV > r = numeric(6) > for (i in 1:6) { > r[i] = 2*U[i] > } > r ## results for each patient > } > ) > result > } > > > -----Original Message----- > From: Liaw, Andy [mailto:andy_liaw at merck.com] > Sent: Wednesday, June 28, 2006 10:23 AM > To: Afshartous, David; r-help at stat.math.ethz.ch > Subject: RE: [R] sapply question > > If you attached files, none made it through the mailing list. > > Can't you try posting a simplified version of the problem > instead? Try > to make it as easy for others to help you as possible will > maximize the > chance you get some useful response. Asking people to read > code you've > attached in files isn't helping, especially if the code is > long and make > use of data not accessible to anyone but you. > > In general, if you want something like tapply, but need several > variables instead of one, you can use something like: > > sapply(split(dataFrame, factor_to_split_by), function(...)) > > HTH, > Andy > > From: Afshartous, David > > > > sent this to the list yesterday but didn't see it listed in > the daily > > summary ... apologies if you receive it twice ... > > > > ________________________________ > > > > From: Afshartous, David > > Sent: Tuesday, June 27, 2006 10:02 AM > > To: 'r-help at stat.math.ethz.ch' > > Subject: sapply question > > > > > > All: > > > > I'm trying to use sapply to break up data within another function. > > (tapply doens't seem to work since I want to access several > variables > > of a data set, not just break up a single variable according to a > > factor.) I tried the R documentation but that wasn't much help on > > this. > > > > The first function in the attached file works, breaking up the data > > according to patient number (6 observations per 24 > patients) via a for > > > loop i=1,..24. > > > > The second function is an attempt to use sapply instead of the first > > "for loop". > > it doesn't load properly and I seem to be absolutely blind > as to why; > > my guess is that it's something "obvious". any help appreciated. > > (necessary data > > attached) > > > > thanks! > > dave > > > > ps: > > I'm on windows and, > > r.2 =100 (input to function) > > dat = comp.CAND.frm (attached file) > > > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help at stat.math.ethz.ch mailing list > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide! > > http://www.R-project.org/posting-guide.html > > > > > > > -------------------------------------------------------------- > ---------- > ------ > Notice: This e-mail message, together with any attachment...{{dropped}}
Andy, That helps, but in my function below I want to perform several calculations before returning a final calculation, hence the {}, and I think that's part of the problem. cheers, dave -----Original Message----- From: Liaw, Andy [mailto:andy_liaw at merck.com] Sent: Wednesday, June 28, 2006 2:15 PM To: Afshartous, David; r-help at stat.math.ethz.ch Subject: RE: [R] sapply question Here's an example, using a data frame with two variables plus a grouping factor:> (dat <- data.frame(x1=1:10, x2=10:1, g=gl(2, 5)))x1 x2 g 1 1 10 1 2 2 9 1 3 3 8 1 4 4 7 1 5 5 6 1 6 6 5 2 7 7 4 2 8 8 3 2 9 9 2 2 10 10 1 2 Now, I want to compute 2 * x1 + x2 for each group in g:> sapply(split(dat, dat$g), function(d) 2 * d$x1 + d$x2)1 2 [1,] 12 17 [2,] 13 18 [3,] 14 19 [4,] 15 20 [5,] 16 21 Does that help? Andy From: Afshartous, David> > Andy, > My apologies for my very poor email. Below is some simplified code. > I think my confusion has more to do w/ embedding the sapplpy within > another function and returning the result, thus this would apply to > your "split" suggestion as well. > However, when I run this the result is still empty. > thanks! > dave > > > creatine.function.new.3 = function(dat) { result = > sapply(levels(dat$Patient_no), function(i) > { > subdat <- subset(dat, dat$Patient_no==i) > U = subdat$UV > r = numeric(6) > for (i in 1:6) { > r[i] = 2*U[i] > } > r ## results for each patient > } > ) > result > } > > > -----Original Message----- > From: Liaw, Andy [mailto:andy_liaw at merck.com] > Sent: Wednesday, June 28, 2006 10:23 AM > To: Afshartous, David; r-help at stat.math.ethz.ch > Subject: RE: [R] sapply question > > If you attached files, none made it through the mailing list. > > Can't you try posting a simplified version of the problem instead? > Try to make it as easy for others to help you as possible will > maximize the chance you get some useful response. Asking people to > read code you've attached in files isn't helping, especially if the > code is long and make use of data not accessible to anyone but you. > > In general, if you want something like tapply, but need several > variables instead of one, you can use something like: > > sapply(split(dataFrame, factor_to_split_by), function(...)) > > HTH, > Andy > > From: Afshartous, David > > > > sent this to the list yesterday but didn't see it listed in > the daily > > summary ... apologies if you receive it twice ... > > > > ________________________________ > > > > From: Afshartous, David > > Sent: Tuesday, June 27, 2006 10:02 AM > > To: 'r-help at stat.math.ethz.ch' > > Subject: sapply question > > > > > > All: > > > > I'm trying to use sapply to break up data within another function. > > (tapply doens't seem to work since I want to access several > variables > > of a data set, not just break up a single variable according to a > > factor.) I tried the R documentation but that wasn't much help on > > this. > > > > The first function in the attached file works, breaking up the data > > according to patient number (6 observations per 24 > patients) via a for > > > loop i=1,..24. > > > > The second function is an attempt to use sapply instead of the first > > "for loop". > > it doesn't load properly and I seem to be absolutely blind > as to why; > > my guess is that it's something "obvious". any help appreciated. > > (necessary data > > attached) > > > > thanks! > > dave > > > > ps: > > I'm on windows and, > > r.2 =100 (input to function) > > dat = comp.CAND.frm (attached file) > > > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help at stat.math.ethz.ch mailing list > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide! > > http://www.R-project.org/posting-guide.html > > > > > > > -------------------------------------------------------------- > ---------- > ------ > Notice: This e-mail message, together with any attachments, contains > information of Merck & Co., Inc. (One Merck Drive, Whitehouse Station,> New Jersey, USA 08889), and/or its affiliates (which may be known > outside the United States as Merck Frosst, Merck Sharp & Dohme or MSD > and in Japan, as Banyu) that may be confidential, proprietary > copyrighted and/or legally privileged. It is intended solely for the > use of the individual or entity named on this message. If you are not> the intended recipient, and have received this message in error, > please notify us immediately by reply e-mail and then delete it from > your system. > -------------------------------------------------------------- > ---------- > ------ > > >------------------------------------------------------------------------ ------ Notice: This e-mail message, together with any attachments, contains information of Merck & Co., Inc. (One Merck Drive, Whitehouse Station, New Jersey, USA 08889), and/or its affiliates (which may be known outside the United States as Merck Frosst, Merck Sharp & Dohme or MSD and in Japan, as Banyu) that may be confidential, proprietary copyrighted and/or legally privileged. It is intended solely for the use of the individual or entity named on this message. If you are not the intended recipient, and have received this message in error, please notify us immediately by reply e-mail and then delete it from your system. ------------------------------------------------------------------------ ------