Reputation: 8818
I am trying to convert all the columns of a dataframe to factors. However, the conversion to factors make all my entries become string
instead of numeric
. See example:
df <- data.frame(a = c(1,2,3), b = c(4,5,6))
df_factor <- apply(df, 2, factor)
This is what I get:
#> df_factor
# a b
#[1,] "1" "4"
#[2,] "2" "5"
#[3,] "3" "6"
Would you have an idea as to where the problem is coming from?
Thanks,
Upvotes: 1
Views: 91
Reputation: 12819
apply(df, 2, factor)
changes your dataframe to a matrix. Use this instead:
df_factor <- as.data.frame(lapply(df, factor))
Upvotes: 1