I am trying to convert a factor to character so that I can relevel. The conversion works for data.frames but does not work in data.table. I know that I can use as.character( ) to convert factors in data.frames but can't figure it out in data.table (Convert data.frame columns from factors to characters)
Here is an example
library( data.table )
mtcars$name <- as.factor( rownames( mtcars ) )
m <- data.table( mtcars )
So this is the fourth row of the factor
m[ 4 , "name" ]
when data.table converts it to character it makes it a number
as.character( m[ 4 , "name" ] )
It works in a data.frame - this is what I want
as.character( mtcars[ 4 , "name" ] )
I need to get this to work so I can relevel the factor. I want to specify which is the reference category by name
m <- within(m, name <- relevel(name, ref = as.character( m[ 4 , "name" ]) ))
It works with a data.frame
mtcars <- within(mtcars, name <- relevel(name, ref = as.character( mtcars[ 4 , "name" ]) ))