[R] Basic question - more efficient method than loop?
Johannes Huesing
johannes at huesing.name
Mon Jun 28 18:31:20 CEST 2010
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")
More information about the R-help
mailing list