I'm guessing there's a more efficient way to do the following using the index features of R. Appreciate any thoughts.... for (i in 1:nrow(dbs1)){ if(dbs1$Payor[i] %in% Payor.Group.Medicaid) dbs1$Payor.Group[i] "Medicaid" if(dbs1$Payor[i] %in% Payor.Group.Medicare) dbs1$Payor.Group[i] "Medicare" if(dbs1$Payor[i] %in% Payor.Group.Commercial) dbs1$Payor.Group[i] "Commercial" if(dbs1$Payor[i] %in% Payor.Group.Workers.Comp) dbs1$Payor.Group[i] "Workers Comp" if(dbs1$Payor[i] %in% Payor.Group.Self.Pay) dbs1$Payor.Group[i] = "Self Pay" if(dbs1$Adm.Source[i] %in% Adm.Source.Group.Newborn) dbs1$Adm.Source.Group[i] = "Newborn" if(dbs1$Adm.Source[i] %in% Adm.Source.Group.ED) dbs1$Adm.Source.Group[i] = "ED" if(dbs1$Adm.Source[i] %in% Adm.Source.Group.Routine) dbs1$Adm.Source.Group[i] = "Routine" if(dbs1$Adm.Source[i] %in% Adm.Source.Group.Transfer) dbs1$Adm.Source.Group[i] = "Transfer" } -- View this message in context: http://r.789695.n4.nabble.com/Basic-question-more-efficient-method-than-loop-tp2271096p2271096.html Sent from the R help mailing list archive at Nabble.com.
Allan Engelhardt
2010-Jun-28 16:18 UTC
[R] Basic question - more efficient method than loop?
On 28/06/10 16:46, GL wrote:> I'm guessing there's a more efficient way to do the following using the index > features of R. Appreciate any thoughts.... > > for (i in 1:nrow(dbs1)){ > if(dbs1$Payor[i] %in% Payor.Group.Medicaid) dbs1$Payor.Group[i] > "Medicaid" >Try something like dbs1$Payor.Group[dbs1$Payor %in% Payor.Group.Medicaid]<- "Medicaid" etc. to get started. Hope this helps. Allan> [...] >
Perfect. Thanks! -- View this message in context: http://r.789695.n4.nabble.com/Basic-question-more-efficient-method-than-loop-tp2271096p2271153.html Sent from the R help mailing list archive at Nabble.com.
Johannes Huesing
2010-Jun-28 16:31 UTC
[R] Basic question - more efficient method than loop?
GL <pflugg at shands.ufl.edu> [Mon, Jun 28, 2010 at 05:46:13PM CEST]:> > I'm guessing there's a more efficient way to do the following using the index > features of R. Appreciate any thoughts....1st thought: ifelse()> > for (i in 1:nrow(dbs1)){ > if(dbs1$Payor[i] %in% Payor.Group.Medicaid) dbs1$Payor.Group[i] > "Medicaid"within(dbs1, Payor.Group <- ifelse(Payor %in% Payor.Group.Medicaid, "Medicaid", ifelse( and so on )) 2nd thought: library(car); ?recode 3rd thought (untested and contrary to the spirit of R): lst <- list("Medicare", "Commercial", "Workers.Comp", etc. ); codePayor <- function(lst) { if (length(lst) == 0) "" else ifelse(dbs1$Payor %in% eval(parse(paste("Payor.Group", lst[[1]], sep="."))), lst[[1]], codePayor(lst[-1])) } dbs1$Payor.Group <- codePayor(lst) -- Johannes H?sing There is something fascinating about science. One gets such wholesale returns of conjecture mailto:johannes at huesing.name from such a trifling investment of fact. http://derwisch.wikidot.com (Mark Twain, "Life on the Mississippi")
Charles C. Berry
2010-Jun-28 16:31 UTC
[R] Basic question - more efficient method than loop?
On Mon, 28 Jun 2010, GL wrote:> > I'm guessing there's a more efficient way to do the following using the index > features of R. Appreciate any thoughts....Use the levels( dbs1$Payor.Group ) <- new.levels idiom after dbs1$Payor.Group <- factor( dbs1$Payor ) See ?levels and example( levels ) Note the 'combine some levels' example. HTH, Chuck> > for (i in 1:nrow(dbs1)){ > if(dbs1$Payor[i] %in% Payor.Group.Medicaid) dbs1$Payor.Group[i] > "Medicaid" > if(dbs1$Payor[i] %in% Payor.Group.Medicare) dbs1$Payor.Group[i] > "Medicare" > if(dbs1$Payor[i] %in% Payor.Group.Commercial) dbs1$Payor.Group[i] > "Commercial" > if(dbs1$Payor[i] %in% Payor.Group.Workers.Comp) dbs1$Payor.Group[i] > "Workers Comp" > if(dbs1$Payor[i] %in% Payor.Group.Self.Pay) dbs1$Payor.Group[i] = "Self > Pay" > if(dbs1$Adm.Source[i] %in% Adm.Source.Group.Newborn) > dbs1$Adm.Source.Group[i] = "Newborn" > if(dbs1$Adm.Source[i] %in% Adm.Source.Group.ED) dbs1$Adm.Source.Group[i] > = "ED" > if(dbs1$Adm.Source[i] %in% Adm.Source.Group.Routine) > dbs1$Adm.Source.Group[i] = "Routine" > if(dbs1$Adm.Source[i] %in% Adm.Source.Group.Transfer) > dbs1$Adm.Source.Group[i] = "Transfer" > } > -- > View this message in context: http://r.789695.n4.nabble.com/Basic-question-more-efficient-method-than-loop-tp2271096p2271096.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E mailto:cberry at tajo.ucsd.edu UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901
Johannes Huesing
2010-Jun-28 16:39 UTC
[R] Basic question - more efficient method than loop?
Johannes Huesing <johannes at huesing.name> [Mon, Jun 28, 2010 at 06:31:20PM CEST]: [...]> eval(parse(paste("Payor.Group", lst[[1]], sep="."))),eval(parse(text=paste("Payor.Group", lst[[1]], sep="."))), -- Johannes H?sing There is something fascinating about science. One gets such wholesale returns of conjecture mailto:johannes at huesing.name from such a trifling investment of fact. http://derwisch.wikidot.com (Mark Twain, "Life on the Mississippi")
Hadley Wickham
2010-Jun-28 17:58 UTC
[R] Basic question - more efficient method than loop?
1) Create a table with two columns: payor and payor.group. 2) Merge that table with your original data Hadley On Mon, Jun 28, 2010 at 10:46 AM, GL <pflugg at shands.ufl.edu> wrote:> > I'm guessing there's a more efficient way to do the following using the index > features of R. Appreciate any thoughts.... > > for (i in 1:nrow(dbs1)){ > ? ?if(dbs1$Payor[i] %in% Payor.Group.Medicaid) dbs1$Payor.Group[i] > "Medicaid" > ? ?if(dbs1$Payor[i] %in% Payor.Group.Medicare) dbs1$Payor.Group[i] > "Medicare" > ? ?if(dbs1$Payor[i] %in% Payor.Group.Commercial) dbs1$Payor.Group[i] > "Commercial" > ? ?if(dbs1$Payor[i] %in% Payor.Group.Workers.Comp) dbs1$Payor.Group[i] > "Workers Comp" > ? ?if(dbs1$Payor[i] %in% Payor.Group.Self.Pay) dbs1$Payor.Group[i] = "Self > Pay" > ? ?if(dbs1$Adm.Source[i] %in% Adm.Source.Group.Newborn) > dbs1$Adm.Source.Group[i] = "Newborn" > ? ?if(dbs1$Adm.Source[i] %in% Adm.Source.Group.ED) dbs1$Adm.Source.Group[i] > = "ED" > ? ?if(dbs1$Adm.Source[i] %in% Adm.Source.Group.Routine) > dbs1$Adm.Source.Group[i] = "Routine" > ? ?if(dbs1$Adm.Source[i] %in% Adm.Source.Group.Transfer) > dbs1$Adm.Source.Group[i] = "Transfer" > ? ?} > -- > View this message in context: http://r.789695.n4.nabble.com/Basic-question-more-efficient-method-than-loop-tp2271096p2271096.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >-- Assistant Professor / Dobelman Family Junior Chair Department of Statistics / Rice University http://had.co.nz/