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

2010-06-28 Thread 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
}
-- 
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@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.


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

2010-06-28 Thread Allan Engelhardt


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


[...]



__
R-help@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.


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

2010-06-28 Thread GL

Perfect. Thanks!
-- 
View this message in context: 
http://r.789695.n4.nabble.com/Basic-question-more-efficient-method-than-loop-tp2271096p2271153.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


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

2010-06-28 Thread Johannes Huesing
GL pfl...@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:johan...@huesing.name  from such a trifling investment of fact.  
  
http://derwisch.wikidot.com (Mark Twain, Life on the Mississippi)

__
R-help@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.


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

2010-06-28 Thread Charles C. Berry

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@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:cbe...@tajo.ucsd.edu   UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901

__
R-help@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.


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

2010-06-28 Thread Johannes Huesing
Johannes Huesing johan...@huesing.name [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:johan...@huesing.name  from such a trifling investment of fact.  
  
http://derwisch.wikidot.com (Mark Twain, Life on the Mississippi)

__
R-help@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.


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

2010-06-28 Thread Hadley Wickham
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 pfl...@shands.ufl.edu 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.

 __
 R-help@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.




-- 
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

__
R-help@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.