I would like to convert a dataframe into a matrix in R. The dataframe has more than 30 different variables with different types, some are numeric, some factors and some characters. When converting it into a matrix, I would like to keep all types exactly the same as in the dataframe.
I tried converting it with as.matrix(), see code below (this is just a simple example dataframe with only two variables).
test_df <- data.frame(a = c(1:10), b = c(letters[1:10]))
test_df <- as.matrix(test_df)
typeof(test_df[,1])
typeof(test_df[,2])
Column 'a' in the example has type integer while column 'b' has type factor. I expect each column to keep its type when converting a dataframe into a matrix. However, when I convert it into a matrix, all variables are being converted into type character.