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

Charles C. Berry cberry at tajo.ucsd.edu
Mon Jun 28 18:31:35 CEST 2010


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



More information about the R-help mailing list