Marcel Gerds
2010-Nov-03 10:01 UTC
[R] Recoding -- test whether number begins with a certain number
Dear R community, I have a question concerning recoding of a variable. I have a data set in which there is a variable devoted to the ISCO code describing the occupation of this certain individual (http://www.ilo.org/public/english/bureau/stat/isco/isco88/major.htm). Every type of occupation begins with a number and every number added to this number describes th occupation more detailed. Now my problem: I want to recode this variable in a way that every value beginning with a certain number is labeled as the respective category. For example, that all values of this variable beginning with a 6 is labeled as "agri". My problem is that I cannot find a test which I can use for that purpose. I would really appreciate any help on that subject. Thank you. Best regards Marcel Gerds
Barry Rowlingson
2010-Nov-03 11:37 UTC
[R] Recoding -- test whether number begins with a certain number
On Wed, Nov 3, 2010 at 10:01 AM, Marcel Gerds <Marcel.Gerds at gmx.de> wrote:> ?Dear R community, > > I have a question concerning recoding of a variable. I have a data set > in which there is a variable devoted to the ISCO code describing the > occupation of this certain individual > (http://www.ilo.org/public/english/bureau/stat/isco/isco88/major.htm). > Every type of occupation begins with a number and every number added to > this number describes th occupation more detailed. > Now my problem: I want to recode this variable in a way that every value > beginning with a certain number is labeled as the respective category. > For example, that all values of this variable beginning with a 6 is > labeled as "agri". > My problem is that I cannot find a test which I can use for that purpose. > > I would really appreciate any help on that subject. Thank you.If it's a numeric variable, convert to character with 'as.character'. Then check the first character with substr(x,1,1). Then create a factor and set the levels... > z=as.integer(runif(10,0,100)) > z [1] 26 92 47 99 2 98 15 21 58 82 > zc=factor(substr(as.character(z),1,1)) > zc [1] 2 9 4 9 2 9 1 2 5 8 Levels: 1 2 4 5 8 9 > levels(zc)=c("Foo","Bar","Baz","Qux","Quux","Quuux") > zc [1] Bar Quuux Baz Quuux Bar Quuux Foo Bar Qux Quux Levels: Foo Bar Baz Qux Quux Quuux > data.frame(z=z,zc=zc) z zc 1 26 Bar 2 92 Quuux 3 47 Baz 4 99 Quuux 5 2 Bar 6 98 Quuux 7 15 Foo 8 21 Bar 9 58 Qux 10 82 Quux Now all the 9-somethings are Quuux, the 2's are Bar etc etc. Barry