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

From: Johannes Huesing <johannes_at_huesing.name>
Date: Mon, 28 Jun 2010 18:31:20 +0200

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")

______________________________________________
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.
Received on Mon 28 Jun 2010 - 16:33:40 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 28 Jun 2010 - 16:50:42 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive