Good morning, I am trying to recode the values of a column in a data frame and, instead of using a long series of if/else statements I would like to use a switch (as it is, for instance, in the C programming language). I have a column of this type: AT BB B1 B1 CC CC T6 B1 CC BB TT AT AT AT TT BB ... and I need to change this coding (if it's AT then it should be A1, if it's BB then it should be A2, and so on ...) I was trying to use switch in the following way: switch(column_name, 'AT'='A1' 'BB'='A2' ...) but it's not working. The R help is particularly cryptic on this point. Can anyone help me? Thank you very much, Filippo Biscarini University of Wageningen (The Netherlands) [[alternative HTML version deleted]]
is this what're you looking for: cn <- c("AT", "BB", "B1", "AT", "B1") sapply(cn, function(val) switch(val, "AT" = "A1", "BB" = "A2", "B1" = "A3")) I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Biscarini, Filippo" <Filippo.Biscarini at wur.nl> To: <r-help at stat.math.ethz.ch> Sent: Monday, November 20, 2006 3:47 PM Subject: [R] how to use the switch statement> > Good morning, > > I am trying to recode the values of a column in a data frame and, > instead of using a long series of if/else statements I would like to > use > a switch (as it is, for instance, in the C programming language). > > I have a column of this type: > > AT > BB > B1 > B1 > CC > CC > T6 > B1 > CC > BB > TT > AT > AT > AT > TT > BB > ... > > and I need to change this coding (if it's AT then it should be A1, > if > it's BB then it should be A2, and so on ...) > > I was trying to use switch in the following way: > > switch(column_name, > 'AT'='A1' > 'BB'='A2' > ...) > > but it's not working. > The R help is particularly cryptic on this point. > > Can anyone help me? > > Thank you very much, > > Filippo Biscarini > > University of Wageningen (The Netherlands) > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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 > and provide commented, minimal, self-contained, reproducible code. >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
It doesn't work because switch is not vectorized. This is a job for match(). Here's an example, recoding a to a1 b to b1 and so on, up to e, e1.> foo <- sample(letters[1:5],10,replace=TRUE) > foo[1] "e" "b" "b" "e" "a" "c" "a" "c" "d" "e"> tmpnew <- c('a1','b1','c1','d1','e1') > tmpold <- letters[1:5] > tmpnew[match(foo,tmpold)][1] "e1" "b1" "b1" "e1" "a1" "c1" "a1" "c1" "d1" "e1" -Don At 3:47 PM +0100 11/20/06, Biscarini, Filippo wrote:>Good morning, > >I am trying to recode the values of a column in a data frame and, >instead of using a long series of if/else statements I would like to use >a switch (as it is, for instance, in the C programming language). > >I have a column of this type: > >AT >BB >B1 >B1 >CC >CC >T6 >B1 >CC >BB >TT >AT >AT >AT >TT >BB > ... > >and I need to change this coding (if it's AT then it should be A1, if >it's BB then it should be A2, and so on ...) > >I was trying to use switch in the following way: > >switch(column_name, >'AT'='A1' >'BB'='A2' >...) > >but it's not working. >The R help is particularly cryptic on this point. > >Can anyone help me? > >Thank you very much, > >Filippo Biscarini > >University of Wageningen (The Netherlands) > > [[alternative HTML version deleted]] > >______________________________________________ >R-help at 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 >and provide commented, minimal, self-contained, reproducible code.-- -------------------------------------- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA
Filippo Can't help you with switch(), but you could use ifelse() (as opposed to "a long series of if/else statements") or factor levels. For the latter Assume your data is column 1 of temp paste('A', as.numeric(factor(temp[,1])), sep='') This doesn't give quite what you want since the factor levels are, by default, assigned alphabetically. So try paste('A', as.numeric(factor(temp[,1], levels=c('AT','BB','TT','B1','CC','T6'))), sep='') HTH Peter Alspach> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of > Biscarini, Filippo > Sent: Tuesday, 21 November 2006 3:48 a.m. > To: r-help at stat.math.ethz.ch > Subject: [R] how to use the switch statement > > > Good morning, > > I am trying to recode the values of a column in a data frame > and, instead of using a long series of if/else statements I > would like to use a switch (as it is, for instance, in the C > programming language). > > I have a column of this type: > > AT > BB > B1 > B1 > CC > CC > T6 > B1 > CC > BB > TT > AT > AT > AT > TT > BB > ... > > and I need to change this coding (if it's AT then it should > be A1, if it's BB then it should be A2, and so on ...) > > I was trying to use switch in the following way: > > switch(column_name, > 'AT'='A1' > 'BB'='A2' > ...) > > but it's not working. > The R help is particularly cryptic on this point. > > Can anyone help me? > > Thank you very much, > > Filippo Biscarini > > University of Wageningen (The Netherlands) > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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 > and provide commented, minimal, self-contained, reproducible code. >______________________________________________________ The contents of this e-mail are privileged and/or confidenti...{{dropped}}
convert your.data.frame$x to ?factor and ?levels> x <- readLines()AT BB B1 B1 CC CC T6 B1 CC BB TT AT AT AT TT BB # you would start here with your.data.frame$x> x <- factor(x,levels=unique(x)) # preserve ordering > x[1] AT BB B1 B1 CC CC T6 B1 CC BB TT AT AT AT TT BB Levels: AT BB B1 CC T6 TT> levels(x) <- paste("A",seq(levels(x)),sep="") > x[1] A1 A2 A3 A3 A4 A4 A5 A3 A4 A2 A6 A1 A1 A1 A6 A2 Levels: A1 A2 A3 A4 A5 A6 HTH Biscarini, Filippo wrote:> Good morning, > > I am trying to recode the values of a column in a data frame and, > instead of using a long series of if/else statements I would like to use > a switch (as it is, for instance, in the C programming language). > > I have a column of this type: > > AT > BB > B1 > B1 > CC > CC > T6 > B1 > CC > BB > TT > AT > AT > AT > TT > BB > ... > > and I need to change this coding (if it's AT then it should be A1, if > it's BB then it should be A2, and so on ...) > > I was trying to use switch in the following way: > > switch(column_name, > 'AT'='A1' > 'BB'='A2' > ...) > > but it's not working. > The R help is particularly cryptic on this point. > > Can anyone help me? > > Thank you very much, > > Filippo Biscarini > > University of Wageningen (The Netherlands)