numerical values of the column of a matrix getting modified when converting into data.frame

r

Solution

> tmp <- data.frame(cbind(1:10,rep("aa",10)))
> str(tmp)
'data.frame':   10 obs. of  2 variables:
 $ X1: Factor w/ 10 levels "1","10","2","3",..: 1 3 4 5 6 7 8 9 10 2
 $ X2: Factor w/ 1 level "aa": 1 1 1 1 1 1 1 1 1 1

As you can see above, `tmp$X1` got converted into a factor, which is what's causing the behaviour you're seeing.

Try:

tmp[,1] <- as.numeric(as.character(tmp[,1]))

Problem

Running on R 2.13, I want to have a data.frame of several column, the first one being of numeric type, the others of character type. When I am creating my object, the values of the first column are getting transformed in a way that I don't expect or understand. Please see the code below. tmp <- cbind(1:10,rep("aa",10)) tmp ``` [,1] [,2] [1,] "1" "aa" [2,] "2" "aa" [3,] "3" "aa" [4,] "4" "aa" [5,] "5" "aa" [6,] "6" "aa" [7,] "7" "aa" [8,] "8" "aa" [9,] "9" "aa" [10,] "10" "aa" ``` tmp <- data.frame(tmp) tmp ``` X1 X2 1 1 aa 2 2 aa 3 3 aa 4 4 aa 5 5 aa 6 6 aa 7 7 aa 8 8 aa 9 9 aa 10 10 aa ``` tmp[,1] <- as.numeric(tmp[,1]) tmp ``` X1 X2 1 1 aa 2 3 aa 3 4 aa 4 5 aa 5 6 aa 6 7 aa 7 8 aa 8 9 aa 9 10 aa 10 2 aa ``` For some reason, the values of the first column are getting changed. I must be doing something obviously wrong here, can someone point me a workaround?

Original source