[R] converting column to factor *within* a data frame

Simon Fear Simon.Fear at synequanon.com
Wed Nov 5 10:11:11 CET 2003

Your problem is with scoping, not the conversion per se:

>  > attach(ngrad)
>  > is.factor(STNW)
> [1] FALSE

At this moment, STNW is the same as ngrad$STNW

>  > ngrad$STNW<-factor(STNW)  ## doesn't work

Yes it does work, try looking at is.factor(ngrad$STNW)

>  > is.factor(STNW)
> [1] FALSE

After you assign to ngrad$STNW, it is no longer the same
thing as the attached STNW. You would need to detach
and re-attach ngrad for this to be so. There's no
automatic synchronisation between the attached STNW 
and ngrad$STNW; changing one will not change the other.

My advice is: never use attach() if you can help it. It's an 
accident waiting to happen. Get used to typing
dataFrame$varname instead of just varname - that way
you will always get what you expect. Or use with() instead 
of attach() in almost every case. 

Simon Fear 
Senior Statistician 
Syne qua non Ltd 
Tel: +44 (0) 1379 644449 
Fax: +44 (0) 1379 644445 
email: Simon.Fear at synequanon.com 
web: http://www.synequanon.com 
Number of attachments included with this message: 0 
This message (and any associated files) is confidential and\...{{dropped}}

More information about the R-help mailing list