Hi all I am trying to do a simple recode which I am stumbling on. I figure there must be any easy way but haven't come across it. Given data of A","B","C","D","E","A" it would be nice to recode this into say three categories ie A and B becomes "Treat1", C becomes "Treat 2" and E becomes "Treat 3". I tried the car library but it didn't seem to like text. Also it seems you can only recode into two categories. library(car) x<-c("A","B","C","D","E","A") x recode(x, "c(A,B)='Treat 1'; else='Treat 2'") recode(x, "1:2='A'; 3='B'") In Microsoft access a simple way of recoding is to join two tables in a query any simple way of doing the same in R? Kind regards Andy Andrew McFadden MVS BVSc Incursion Investigator Investigation & Diagnostic Centres - Wallaceville Biosecurity New Zealand Ministry of Agriculture and Forestry Phone 04 894 5600 Fax 04 894 4973 Mobile 029 894 5611 Postal address: Investigation and Diagnostic Centre- Wallaceville Box 40742 Ward St Upper Hutt ######################################################################## This email message and any attachment(s) is intended solely for the addressee(s) named above. The information it contains is confidential and may be legally privileged. Unauthorised use of the message, or the information it contains, may be unlawful. If you have received this message by mistake please call the sender immediately on 64 4 8940100 or notify us by return email and erase the original message and attachments. Thank you. The Ministry of Agriculture and Forestry accepts no responsibility for changes made to this email or to any attachments after transmission from the office. ######################################################################## [[alternative HTML version deleted]]
Here is one way> x <- c("A", "B", "C", "D", "E", "A") > x[1] "A" "B" "C" "D" "E" "A"> f <- factor(x) > levels(f)[1] "A" "B" "C" "D" "E"> levels(f) <- c(rep("Treat1",2), rep("Treat2", 3)) > f[1] Treat1 Treat1 Treat2 Treat2 Treat2 Treat1 Levels: Treat1 Treat2>If you really want the charactre variable you can put> x <- as.character(f)_____ That's fascinating about Microsoft Access, but, uh... never mind. Bill Venables http://www.cmis.csiro.au/bill.venables/ -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Andrew McFadden Sent: Thursday, 2 April 2009 9:23 AM To: r-help at r-project.org Subject: [R] Recode of text variables Hi all I am trying to do a simple recode which I am stumbling on. I figure there must be any easy way but haven't come across it. Given data of A","B","C","D","E","A" it would be nice to recode this into say three categories ie A and B becomes "Treat1", C becomes "Treat 2" and E becomes "Treat 3". I tried the car library but it didn't seem to like text. Also it seems you can only recode into two categories. library(car) x<-c("A","B","C","D","E","A") x recode(x, "c(A,B)='Treat 1'; else='Treat 2'") recode(x, "1:2='A'; 3='B'") In Microsoft access a simple way of recoding is to join two tables in a query any simple way of doing the same in R? Kind regards Andy Andrew McFadden MVS BVSc Incursion Investigator Investigation & Diagnostic Centres - Wallaceville Biosecurity New Zealand Ministry of Agriculture and Forestry Phone 04 894 5600 Fax 04 894 4973 Mobile 029 894 5611 Postal address: Investigation and Diagnostic Centre- Wallaceville Box 40742 Ward St Upper Hutt ######################################################################## This email message and any attachment(s) is intended solely for the addressee(s) named above. The information it contains is confidential and may be legally privileged. Unauthorised use of the message, or the information it contains, may be unlawful. If you have received this message by mistake please call the sender immediately on 64 4 8940100 or notify us by return email and erase the original message and attachments. Thank you. The Ministry of Agriculture and Forestry accepts no responsibility for changes made to this email or to any attachments after transmission from the office. ######################################################################## [[alternative HTML version deleted]] ______________________________________________ R-help at 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.
On 2/04/2009, at 12:22 PM, Andrew McFadden wrote:> Hi all > > I am trying to do a simple recode which I am stumbling on. I figure > there must be any easy way but haven't come across it. > > Given data of A","B","C","D","E","A" it would be nice to recode this > into say three categories ie A and B becomes "Treat1", C becomes > "Treat > 2" and E becomes "Treat 3". > > I tried the car library but it didn't seem to like text. Also it seems > you can only recode into two categories. > > > library(car) > > x<-c("A","B","C","D","E","A") > x > recode(x, "c(A,B)='Treat 1'; else='Treat 2'") > recode(x, "1:2='A'; 3='B'") > > In Microsoft access a simple way of recoding is to join two tables > in a > query any simple way of doing the same in R?y <- c("Treat 1","Treat 1","Treat 2","Blechh!","Treat 3")[match (x,LETTERS[1:5])] is one way of doing it. There are a brazillion others (using factors, levels, and labels for instance). Some of them may be sexier than my suggestion above. Note that D gets recoded as ``Blechh!'' in the my suggestion, since you didn't specify what you wanted to happen to D. If you want D to stay as D, replace "Blechh!" by "D"; or if you want it to becoming ``missing'' replace "Blechh!" by NA. cheers, Rolf Turner ###################################################################### Attention:\ This e-mail message is privileged and confid...{{dropped:9}}
Dear Andy, There are two problems here: One is that you need to enclose character values in quotes; the other is a bug in recode(), which is confused by the values "Treat 1" and "Treat 2" and shouldn't be. Here's a work-around [until tomorrow, when I'll fix recode()]:> as.character(recode(x, "c('A', 'B')='Treat 1'; else='Treat 2'",as.factor=TRUE)) [1] "Treat 1" "Treat 1" "Treat 2" "Treat 2" "Treat 2" "Treat 1" And, no, you can recode into as many different values as you want -- e.g.,> recode(x, "c('A', 'B')='a'; c('C', 'D') = 'b'; else='c'")[1] "a" "a" "b" "b" "c" "a" Finally, I'm not sure what recode(x, "1:2='A'; 3='B'") is supposed to do, inasmuch as x is a character, not numeric, vector. Thanks for bringing the bug to my attention. John> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]On> Behalf Of Andrew McFadden > Sent: April-01-09 7:23 PM > To: r-help at r-project.org > Subject: [R] Recode of text variables > > Hi all > > I am trying to do a simple recode which I am stumbling on. I figure > there must be any easy way but haven't come across it. > > Given data of A","B","C","D","E","A" it would be nice to recode this > into say three categories ie A and B becomes "Treat1", C becomes "Treat > 2" and E becomes "Treat 3". > > I tried the car library but it didn't seem to like text. Also it seems > you can only recode into two categories. > > > library(car) > > x<-c("A","B","C","D","E","A") > x > recode(x, "c(A,B)='Treat 1'; else='Treat 2'") > recode(x, "1:2='A'; 3='B'") > > In Microsoft access a simple way of recoding is to join two tables in a > query any simple way of doing the same in R? > > > > Kind regards > > Andy > > Andrew McFadden MVS BVSc > Incursion Investigator > Investigation & Diagnostic Centres - Wallaceville Biosecurity New > Zealand Ministry of Agriculture and Forestry > > Phone 04 894 5600 Fax 04 894 4973 Mobile 029 894 5611 Postal address: > Investigation and Diagnostic Centre- Wallaceville Box 40742 Ward St > Upper Hutt > > > ######################################################################## > This email message and any attachment(s) is intended solely for the > addressee(s) named above. The information it contains is confidential > and may be legally privileged. Unauthorised use of the message, or the > information it contains, may be unlawful. If you have received this > message by mistake please call the sender immediately on 64 4 8940100 > or notify us by return email and erase the original message and > attachments. Thank you. > > The Ministry of Agriculture and Forestry accepts no responsibility for > changes made to this email or to any attachments after transmission from > the office. > ######################################################################## > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
doBy and memisc packages have recoding functions as well, e.g.> library(doBy) > x<-c("A","B","C","D","E","A") > out <- recodevar(x, list(c("A", "B"), c("C", "D", "E")), list("Treat 1", "Treat 2")) > recodevar(x, list(c("A", "B"), c("C", "D", "E")), list("Treat 1", "Treat 2"))[1] "Treat 1" "Treat 1" "Treat 2" "Treat 2" "Treat 2" "Treat 1" It would be possible to use the merge function or the sqldf package do something similar to MS Access. On Wed, Apr 1, 2009 at 7:22 PM, Andrew McFadden <Andrew.McFadden at maf.govt.nz> wrote:> Hi all > > I am trying to do a simple recode which I am stumbling on. I figure > there must be any easy way but haven't come across it. > > Given data of A","B","C","D","E","A" it would be nice to recode this > into say three categories ie A and B becomes "Treat1", C becomes "Treat > 2" and E becomes "Treat 3". > > I tried the car library but it didn't seem to like text. Also it seems > you can only recode into two categories. > > > library(car) > > x<-c("A","B","C","D","E","A") > x > recode(x, "c(A,B)='Treat 1'; else='Treat 2'") > recode(x, "1:2='A'; 3='B'") > > In Microsoft access a simple way of recoding is to join two tables in a > query any simple way of doing the same in R? > > > > Kind regards > > Andy > > Andrew McFadden MVS BVSc > Incursion Investigator > Investigation & Diagnostic Centres - Wallaceville Biosecurity New > Zealand Ministry of Agriculture and Forestry > > Phone 04 894 5600 Fax 04 894 4973 Mobile 029 894 5611 Postal address: > Investigation and Diagnostic Centre- Wallaceville Box 40742 Ward St > Upper Hutt > > > ######################################################################## > This email message and any attachment(s) is intended solely for the > addressee(s) named above. The information it contains is confidential > and may be legally privileged. ?Unauthorised use of the message, or the > information it contains, may be unlawful. If you have received this > message by mistake please call the sender immediately on 64 4 8940100 > or notify us by return email and erase the original message and > attachments. Thank you. > > The Ministry of Agriculture and Forestry accepts no responsibility for > changes made to this email or to any attachments after transmission from > the office. > ######################################################################## > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. >