[R] odd behavior of data.matrix()

Brian Diggs diggsb at ohsu.edu
Thu Aug 18 22:59:13 CEST 2011


On 8/18/2011 1:40 PM, Alexander Schwall wrote:
> Hi R community,
>
> I have been trying to figure out why R is reversing the order of rows after
> I run data.matrix()
>
> Here is my data:
>
> df<-structure(list(itmID = c(1L, 2L, 1L, 2L, 1L, 2L), variable =
> structure(c(1L,
>
> 1L, 2L, 2L, 3L, 3L), .Label = c("3", "2", "1"), class = "factor"),
>      value = c(0.7, 0.52, -1.16, -0.91, 0, 0)), .Names = c("itmID",
>
> "variable", "value"), row.names = c(NA, -6L), class = "data.frame")(NA,
> -6L), class = "data.frame")
>
> The data looks like this now
>
>
>> df
>
>    itmID variable value
> 1     1        3  0.70
> 2     2        3  0.52
> 3     1        2 -1.16
> 4     2        2 -0.91
> 5     1        1  0.00
> 6     2        1  0.00
>
>
> however after running data.matrix(df) I get:
>
>
>       itmID variable value
> [1,]     1        1  0.70
> [2,]     2        1  0.52
> [3,]     1        2 -1.16
> [4,]     2        2 -0.91
> [5,]     1        3  0.00
> [6,]     2        3  0.00
>
>
> Note, only the order of the column "variable" as been reversed 332211
> to 112233, not the order of the other columns. So, the data is now out
> of order.

 From the data.matrix documentation:

"Factors and ordered factors are replaced by their internal codes."

Note that variable is a factor:

 > str(df)
'data.frame':   6 obs. of  3 variables:
  $ itmID   : int  1 2 1 2 1 2
  $ variable: Factor w/ 3 levels "3","2","1": 1 1 2 2 3 3
  $ value   : num  0.7 0.52 -1.16 -0.91 0 0

So what you see as "3" is level 1, "2" is level 2, and "1" is level 3, 
which is consistent with what you are seeing as the result of 
data.matrix(df)

df$variable <- as.numeric(as.character(df$variable))

will change it to an actual number

 > str(df)
'data.frame':   6 obs. of  3 variables:
  $ itmID   : int  1 2 1 2 1 2
  $ variable: num  3 3 2 2 1 1
  $ value   : num  0.7 0.52 -1.16 -0.91 0 0
 > data.matrix(df)
      itmID variable value
[1,]     1        3  0.70
[2,]     2        3  0.52
[3,]     1        2 -1.16
[4,]     2        2 -0.91
[5,]     1        1  0.00
[6,]     2        1  0.00

> Any thoughts. What I am doing wrong?
>
>
> Thanks
>
> Alexander
>
> 	[[alternative HTML version deleted]]
>


-- 
Brian S. Diggs, PhD
Senior Research Associate, Department of Surgery
Oregon Health & Science University



More information about the R-help mailing list