[R] Simple loop code

RCulloch ross.culloch at dur.ac.uk
Thu Apr 29 13:39:45 CEST 2010


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.



More information about the R-help mailing list