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

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

GL <> [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="."))),



dbs1$Payor.Group <- codePayor(lst)

Johannes Hüsing               There is something fascinating about science. 
                              One gets such wholesale returns of conjecture  from such a trifling investment of fact.                (Mark Twain, "Life on the Mississippi")

______________________________________________ mailing list
PLEASE do read the posting guide
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 Please read the posting guide before posting to the list.

list of date sections of archive