Hi fellow R Users, I find that I typically rewrite my data specific to data in columns, which is by no means efficient and I am struggling to break out of this bad habit and utalise some of the excellent things R can do! I have tried to look at 'for' but I don't really follow it, and I wondered if anyone could help with a simple example using my script so I could follow this and build on it, so for example, wanting to change an ID code from alphanumeric to numeric. The example below works, but takes ages, given I have a lot of IDs, to do manually! Any thoughts on how to create a loop to go through each ID and give them a unique number would be most welcome! Cheers, Ross levels(dat.ID$ID2)[levels(dat.ID$ID2)=='A1']<-1 levels(dat.ID$ID2)[levels(dat.ID$ID2)=='A2']<-2 levels(dat.ID$ID2)[levels(dat.ID$ID2)=='D1']<-3 levels(dat.ID$ID2)[levels(dat.ID$ID2)=='D2']<-4 levels(dat.ID$ID2)[levels(dat.ID$ID2)=='D4']<-5 levels(dat.ID$ID2)[levels(dat.ID$ID2)=='D5']<-6 levels(dat.ID$ID2)[levels(dat.ID$ID2)=='D6']<-7 -- View this message in context: http://r.789695.n4.nabble.com/Simple-loop-code-tp2075322p2075322.html Sent from the R help mailing list archive at Nabble.com.
Try this: factor(dat.ID$ID2, labels = 1:7) On Thu, Apr 29, 2010 at 8:39 AM, RCulloch <ross.culloch@dur.ac.uk> wrote:> > Hi fellow R Users, > > I find that I typically rewrite my data specific to data in columns, which > is by no means efficient and I am struggling to break out of this bad habit > and utalise some of the excellent things R can do! I have tried to look at > 'for' but I don't really follow it, and I wondered if anyone could help > with > a simple example using my script so I could follow this and build on it, so > for example, wanting to change an ID code from alphanumeric to numeric. The > example below works, but takes ages, given I have a lot of IDs, to do > manually! > > Any thoughts on how to create a loop to go through each ID and give them a > unique number would be most welcome! > > Cheers, > > Ross > > > levels(dat.ID$ID2)[levels(dat.ID$ID2)=='A1']<-1 > levels(dat.ID$ID2)[levels(dat.ID$ID2)=='A2']<-2 > levels(dat.ID$ID2)[levels(dat.ID$ID2)=='D1']<-3 > levels(dat.ID$ID2)[levels(dat.ID$ID2)=='D2']<-4 > levels(dat.ID$ID2)[levels(dat.ID$ID2)=='D4']<-5 > levels(dat.ID$ID2)[levels(dat.ID$ID2)=='D5']<-6 > levels(dat.ID$ID2)[levels(dat.ID$ID2)=='D6']<-7 > -- > View this message in context: > http://r.789695.n4.nabble.com/Simple-loop-code-tp2075322p2075322.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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Thanks Henrique, that works! for anyone else as slow as me, just: ##Assign x <- factor(dat.ID$ID2, labels = 1:7) ##Convert to dataframe x <- as.data.frame(x) ##Then bind to your data z <- cbind(y,x) Thanks again, I expected it to be more complicated! Cheers, Ross -- View this message in context: http://r.789695.n4.nabble.com/Simple-loop-code-tp2075322p2075586.html Sent from the R help mailing list archive at Nabble.com.
On Apr 29, 2010, at 10:37 AM, RCulloch wrote:> > Thanks Henrique, > > that works! for anyone else as slow as me, just: > > ##Assign > x <- factor(dat.ID$ID2, labels = 1:7) > ##Convert to dataframe > x <- as.data.frame(x)The more typical methods for converting a factor to a character vector would be: (ff <- factor(substring("statistics", 1:10, 1:10), levels=letters)) levels(ff)[ff] # [1] "s" "t" "a" "t" "i" "s" "t" "i" "c" "s" as.character(ff) # [1] "s" "t" "a" "t" "i" "s" "t" "i" "c" "s"> ##Then bind to your data > z <- cbind(y,x)Oooh. Not a good practice, at least for the newish useR. cbind and rbind create matrices and as a consequence coerce all of their elements to be of the same type. Numeric columns would become character vectors. Not generally a desired result. This would be safer: dat.I$ID2.cf <- as.character( factor(dat.ID$ID2, labels = 1:7) ) -- David.> > Thanks again, I expected it to be more complicated! > > Cheers, > > Ross > -- > View this message in context: http://r.789695.n4.nabble.com/Simple-loop-code-tp2075322p2075586.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.David Winsemius, MD West Hartford, CT