Re: [R] recoding large number of categories (select in SAS)

2005-01-19 Thread Philippe Grosjean
Does
 ?cut
answers to your question?
Best,
Philippe Grosjean
..°}))
 ) ) ) ) )
( ( ( ( (Prof. Philippe Grosjean
 ) ) ) ) )
( ( ( ( (Numerical Ecology of Aquatic Systems
 ) ) ) ) )   Mons-Hainaut University, Pentagone (3D08)
( ( ( ( (Academie Universitaire Wallonie-Bruxelles
 ) ) ) ) )   8, av du Champ de Mars, 7000 Mons, Belgium
( ( ( ( (
 ) ) ) ) )   phone: + 32.65.37.34.97, fax: + 32.65.37.30.54
( ( ( ( (email: [EMAIL PROTECTED]
 ) ) ) ) )
( ( ( ( (web:   http://www.umh.ac.be/~econum
 ) ) ) ) )  http://www.sciviews.org
( ( ( ( (
..
Denis Chabot wrote:
Hi,
I have data on stomach contents. Possible prey species are in the 
hundreds, so a list of prey codes has been in used in many labs doing 
this kind of work.

When comes time to do analyses on these data one often wants to regroup 
prey in broader categories, especially for rare prey.

In SAS you can nest a large number of if-else, or do this more cleanly 
with select like this:
select;
  when (149 = prey =150)   preyGr= 150;
  when (186 = prey = 187)  preyGr= 187;
  when (prey= 438) preyGr= 438;
  when (prey= 430) preyGr= 430;
  when (prey= 436) preyGr= 436;
  when (prey= 431) preyGr= 431;
  when (prey= 451) preyGr= 451;
  when (prey= 461) preyGr= 461;
  when (prey= 478) preyGr= 478;
  when (prey= 572) preyGr= 572;
  when (692 = prey =  695 )   
preyGr= 692;
  when (808 = prey =  826, 830 = prey = 832 )  preyGr= 808;
  when (997 = prey = 998, 792 = prey = 796)  preyGr= 792;
  when (882 = prey = 909)  preyGr= 882;
  when (prey in (999, 125, 994)) preyGr= 9994;
  otherwise preyGr= 1;
end; *select;

The number of transformations is usually much larger than this short 
example.

What is the best way of doing this in R?
Sincerely,
Denis Chabot
__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! 
http://www.R-project.org/posting-guide.html


__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] recoding large number of categories (select in SAS)

2005-01-19 Thread james . holtman




Here is a way of doing it by setting up a matrix of values to test against.
Easier than writing all the 'select' statements.

 x.trans - matrix(c(  # translation matrix; first column is min, second
is max,
+ 149, 150, 150,  # and third is the value to be returned
+ 186, 187, 187,
+ 438, 438, 438,
+ 430, 430, 430,
+ 808, 826, 808,
+ 830, 832, 808,
+ 997, 998, 792,
+ 792, 796, 792), ncol=3, byrow=T)
 colnames(x.trans) - c('min', 'max', 'value')

 x.default -    # default/nomatch value

 x.test - c(150, 149, 148, 438, 997, 791, 795, 810, 820, 834)   # test
data
 #
 # this function will test each value and if between the min/max, return 3
column
 #
 newValues - sapply(x.test, function(x){
+ .value - x.trans[(x = x.trans[,'min'])  (x =
x.trans[,'max']),'value']
+ if (length(.value) == 0) .value - x.default# on no match, take
default
+ .value[1]   # return first value if multiple matches
+ })
 newValues
 [1]  150  150   438  792   792  808  808 

__
James HoltmanWhat is the problem you are trying to solve?
Executive Technical Consultant  --  Office of Technology, Convergys
[EMAIL PROTECTED]
+1 (513) 723-2929



   
  Denis Chabot  
   
  [EMAIL PROTECTED]To:   
r-help@stat.math.ethz.ch  
  .netcc:  
   
  Sent by: Subject:  [R] recoding large 
number of categories (select in SAS)   
  [EMAIL PROTECTED] 
   
  ath.ethz.ch   
   

   

   
  01/19/2005 08:56 AM   
   

   

   




Hi,

I have data on stomach contents. Possible prey species are in the
hundreds, so a list of prey codes has been in used in many labs doing
this kind of work.

When comes time to do analyses on these data one often wants to regroup
prey in broader categories, especially for rare prey.

In SAS you can nest a large number of if-else, or do this more
cleanly with select like this:
select;
   when (149 = prey =150)   preyGr= 150;
   when (186 = prey = 187)  preyGr= 187;
   when (prey= 438) preyGr= 438;
   when (prey= 430) preyGr= 430;
   when (prey= 436) preyGr= 436;
   when (prey= 431) preyGr= 431;
   when (prey= 451) preyGr= 451;
   when (prey= 461) preyGr= 461;
   when (prey= 478) preyGr= 478;
   when (prey= 572) preyGr= 572;
   when (692 = prey =  695 )
preyGr= 692;
   when (808 = prey =  826, 830 = prey = 832 )   preyGr= 808;
   when (997 = prey = 998, 792 = prey = 796) preyGr= 792;
   when (882 = prey = 909)
 preyGr= 882;
   when (prey in (999, 125, 994))
preyGr= 9994;
   otherwise preyGr= 1;
end; *select;

The number of transformations is usually much larger than this short
example.

What is the best way of doing this in R?

Sincerely,

Denis Chabot

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] recoding large number of categories (select in SAS)

2005-01-19 Thread Peter Dalgaard
Philippe Grosjean [EMAIL PROTECTED] writes:

 Does
 
   ?cut
 
 answers to your question?

That's one way, but it tends to get messy to get the names right.

You might consider using the rather little-known variant of levels
assignment:

preyGR - prey # or factor(prey) if it wasn't one already
levels(preyGR) - list(150=149:150,
   187=187:188,
   438=438, 

[...]

   9994=c(999,125,994), 1=NA) 

preyGR[is.na(preyGR)  !is.na(prey)] - 1

This would be roughly as clean as the SAS way, only the otherwise
case got a bit tricky.

 Best,
 
 Philippe Grosjean
 
 ..°}))
   ) ) ) ) )
 ( ( ( ( (Prof. Philippe Grosjean
   ) ) ) ) )
 ( ( ( ( (Numerical Ecology of Aquatic Systems
   ) ) ) ) )   Mons-Hainaut University, Pentagone (3D08)
 ( ( ( ( (Academie Universitaire Wallonie-Bruxelles
   ) ) ) ) )   8, av du Champ de Mars, 7000 Mons, Belgium
 ( ( ( ( (
   ) ) ) ) )   phone: + 32.65.37.34.97, fax: + 32.65.37.30.54
 ( ( ( ( (email: [EMAIL PROTECTED]
   ) ) ) ) )
 ( ( ( ( (web:   http://www.umh.ac.be/~econum
   ) ) ) ) )  http://www.sciviews.org
 ( ( ( ( (
 ..
 
 Denis Chabot wrote:
  Hi,
  I have data on stomach contents. Possible prey species are in the
  hundreds, so a list of prey codes has been in used in many labs
  doing this kind of work.
  When comes time to do analyses on these data one often wants to
  regroup prey in broader categories, especially for rare prey.
  In SAS you can nest a large number of if-else, or do this more
  cleanly with select like this:
  select;
when (149 = prey =150)   preyGr= 150;
when (186 = prey = 187)  preyGr= 187;
when (prey= 438) preyGr= 438;
when (prey= 430) preyGr= 430;
when (prey= 436) preyGr= 436;
when (prey= 431) preyGr= 431;
when (prey= 451) preyGr= 451;
when (prey= 461) preyGr= 461;
when (prey= 478) preyGr= 478;
when (prey= 572) preyGr= 572;
when (692 = prey =  695 )
  preyGr= 692;
when (808 = prey =  826, 830 = prey = 832 )  preyGr= 808;
when (997 = prey = 998, 792 = prey = 796)  preyGr= 792;
when (882 = prey = 909)  preyGr= 882;
when (prey in (999, 125, 994)) preyGr= 9994;
otherwise preyGr= 1;
  end; *select;
  The number of transformations is usually much larger than this short
  example.
  What is the best way of doing this in R?
  Sincerely,
  Denis Chabot
  __
  R-help@stat.math.ethz.ch mailing list
  https://stat.ethz.ch/mailman/listinfo/r-help
  PLEASE do read the posting guide!
  http://www.R-project.org/posting-guide.html
 
 
 __
 R-help@stat.math.ethz.ch mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
 

-- 
   O__   Peter Dalgaard Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics 2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark  Ph: (+45) 35327918
~~ - ([EMAIL PROTECTED]) FAX: (+45) 35327907

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


RE: [R] recoding large number of categories (select in SAS)

2005-01-19 Thread John Fox
Dear Peter et al.,

The recode() function in the car package will also do this kind of thing,
will work even when the ranges include non-integers, and supports an else=
construction.

Regards,
 John


John Fox
Department of Sociology
McMaster University
Hamilton, Ontario
Canada L8S 4M4
905-525-9140x23604
http://socserv.mcmaster.ca/jfox 
 

 -Original Message-
 From: [EMAIL PROTECTED] 
 [mailto:[EMAIL PROTECTED] On Behalf Of Peter Dalgaard
 Sent: Wednesday, January 19, 2005 10:52 AM
 To: Philippe Grosjean
 Cc: r-help@stat.math.ethz.ch; Denis Chabot
 Subject: Re: [R] recoding large number of categories (select in SAS)
 
 Philippe Grosjean [EMAIL PROTECTED] writes:
 
  Does
  
?cut
  
  answers to your question?
 
 That's one way, but it tends to get messy to get the names right.
 
 You might consider using the rather little-known variant of levels
 assignment:
 
 preyGR - prey # or factor(prey) if it wasn't one already
 levels(preyGR) - list(150=149:150,
187=187:188,
438=438, 
 
 [...]
 
9994=c(999,125,994), 1=NA) 
 
 preyGR[is.na(preyGR)  !is.na(prey)] - 1
 
 This would be roughly as clean as the SAS way, only the otherwise
 case got a bit tricky.
 
  Best,
  
  Philippe Grosjean
  
  ..°}))
) ) ) ) )
  ( ( ( ( (Prof. Philippe Grosjean
) ) ) ) )
  ( ( ( ( (Numerical Ecology of Aquatic Systems
) ) ) ) )   Mons-Hainaut University, Pentagone (3D08)
  ( ( ( ( (Academie Universitaire Wallonie-Bruxelles
) ) ) ) )   8, av du Champ de Mars, 7000 Mons, Belgium
  ( ( ( ( (
) ) ) ) )   phone: + 32.65.37.34.97, fax: + 32.65.37.30.54
  ( ( ( ( (email: [EMAIL PROTECTED]
) ) ) ) )
  ( ( ( ( (web:   http://www.umh.ac.be/~econum
) ) ) ) )  http://www.sciviews.org
  ( ( ( ( (
  ..
  
  Denis Chabot wrote:
   Hi,
   I have data on stomach contents. Possible prey species are in the 
   hundreds, so a list of prey codes has been in used in many labs 
   doing this kind of work.
   When comes time to do analyses on these data one often wants to 
   regroup prey in broader categories, especially for rare prey.
   In SAS you can nest a large number of if-else, or do this more 
   cleanly with select like this:
   select;
 when (149 = prey =150)   preyGr= 150;
 when (186 = prey = 187)  preyGr= 187;
 when (prey= 438) preyGr= 438;
 when (prey= 430) preyGr= 430;
 when (prey= 436) preyGr= 436;
 when (prey= 431) preyGr= 431;
 when (prey= 451) preyGr= 451;
 when (prey= 461) preyGr= 461;
 when (prey= 478) preyGr= 478;
 when (prey= 572) preyGr= 572;
 when (692 = prey =  695 )
   preyGr= 692;
 when (808 = prey =  826, 830 = prey = 832 )  
 preyGr= 808;
 when (997 = prey = 998, 792 = prey = 796)  preyGr= 792;
 when (882 = prey = 909)  preyGr= 882;
 when (prey in (999, 125, 994))  
preyGr= 9994;
 otherwise preyGr= 1;
   end; *select;
   The number of transformations is usually much larger than 
 this short 
   example.
   What is the best way of doing this in R?
   Sincerely,
   Denis Chabot
   __
   R-help@stat.math.ethz.ch mailing list 
   https://stat.ethz.ch/mailman/listinfo/r-help
   PLEASE do read the posting guide!
   http://www.R-project.org/posting-guide.html
  
  
  __
  R-help@stat.math.ethz.ch mailing list
  https://stat.ethz.ch/mailman/listinfo/r-help
  PLEASE do read the posting guide! 
  http://www.R-project.org/posting-guide.html
  
 
 -- 
O__   Peter Dalgaard Blegdamsvej 3  
   c/ /'_ --- Dept. of Biostatistics 2200 Cph. N   
  (*) \(*) -- University of Copenhagen   Denmark  Ph: 
 (+45) 35327918
 ~~ - ([EMAIL PROTECTED]) FAX: 
 (+45) 35327907
 
 __
 R-help@stat.math.ethz.ch mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide! 
 http://www.R-project.org/posting-guide.html

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html