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]]

