Q:

How data frame handle data type?

I have encountered an issue that I do not understand and could not find an explanation so far. Here is an example :
x = matrix(data = "test", nrow = 5, ncol = 3)
typeof(x[1, 1])
> "character"

x = as.data.frame(x)
typeof(x[1, 1])
> "integer"

Any idea as to why as.data.frame() coerce data to integer type and how to prevent it from happening ?

A:

The matrix can hold only a single class.  Normally, we use matrix for numeric elements.  Suppose if there is a single element in matrix that is non-numeric, it will convert the whole matrix to character class.  
Regarding the OP's post, we have a matrix with character elements.  Coercing a matrix to data.frame (with as.data.frame), it will be converted to data.frame, but the default option (stringsAsFactors=TRUE) in data.frame for 'character' elements in each column will be to convert it to factor class.  When we use typeof, we get the integer representation of factor.
This can be avoided by using stringsAsFactors=FALSE
 x1 <- as.data.frame(x, stringsAsFactors=FALSE) 

