Basic question - more efficient method than loop?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
GL
Reply | Threaded
Open this post in threaded view
|

Basic question - more efficient method than loop?

GL
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"
    }
Reply | Threaded
Open this post in threaded view
|

Re: Basic question - more efficient method than loop?

cybaea

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

>     [...]
>

______________________________________________
[hidden email] 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.
GL
Reply | Threaded
Open this post in threaded view
|

Re: Basic question - more efficient method than loop?

GL
In reply to this post by GL
Perfect. Thanks!
Reply | Threaded
Open this post in threaded view
|

Re: Basic question - more efficient method than loop?

Johannes Huesing
In reply to this post by GL
GL <[hidden email]> [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:[hidden email]  from such a trifling investment of fact.                
http://derwisch.wikidot.com         (Mark Twain, "Life on the Mississippi")

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Basic question - more efficient method than loop?

cberry
In reply to this post by GL
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.
>
> ______________________________________________
> [hidden email] 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:[hidden email]            UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Basic question - more efficient method than loop?

Johannes Huesing
In reply to this post by Johannes Huesing
Johannes Huesing <[hidden email]> [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:[hidden email]  from such a trifling investment of fact.                
http://derwisch.wikidot.com         (Mark Twain, "Life on the Mississippi")

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Basic question - more efficient method than loop?

Hadley Wickham-2
In reply to this post by GL
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 <[hidden email]> 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.
>
> ______________________________________________
> [hidden email] 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/

______________________________________________
[hidden email] 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.