[R] change the class of an object within with()

François MICHONNEAU francois.michonneau at gmail.com
Tue Jun 27 11:54:22 CEST 2006


Hello,

Can anyone explain me what is wrong in the following code? and in particular why it is not 
possible to change the class of an object thanks to the function with(). Does an 
alternative exist?

xxx <- data.frame(x = c("a","b","c","d","e"), y = c("1","2","3","4","5"))
str(xxx)
with(xxx, {
	x <- as.character(x)
	y <- as.numeric(y)
})
str(xxx) #no effect on the class of x and y

xxx$x <- as.character(xxx$x)
xxx$y <- as.numeric(xxx$y)
str(xxx)

Thanks

François Michonnneau



More information about the R-help mailing list