[R] Basic question - more efficient method than loop?

Hadley Wickham hadley at rice.edu
Mon Jun 28 19:58:26 CEST 2010


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/



More information about the R-help mailing list