MatthewR
MatthewR

Reputation: 2780

Treatment of factors when converting to character in data.table

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

Upvotes: 1

Views: 79

Answers (1)

akrun
akrun

Reputation: 887971

Extract the column, it is still a data.table with one column

as.character(m[4, "name"]$name)
#[1] "Hornet 4 Drive"

The issue is that the column is 1) factor, 2), it is still a data.table (subsetting of data.frame behavior is different as it has drop = TRUE as default behavior, here it is not the case)

We can replicate the same issue with data.frame too

as.character(mtcars[4, "name", drop = FALSE])
#[1] "13"

factors are coerced to integer storage mode values and then only it gets converted to character

Upvotes: 1

Related Questions