data.matrix output is not numeric

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

data.matrix output is not numeric

Ed Siefker
I have a data frame full of integer values.  I need a matrix full of
numeric values.

?data.matrix reads:

     Return the matrix obtained by converting all the variables in a
     data frame to numeric mode and then binding them together as the
     columns of a matrix.

This does not work.

test.df <- data.frame(a=as.integer(c(1,2,3)), b=as.integer(c(4,5,6)))
> class(test.df[[1,1]])
[1] "integer"
> class(data.matrix(test.df)[[1]])
[1] "integer"

What's going on here?

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: data.matrix output is not numeric

Richard M. Heiberger
integer is a subclass of numeric.

> is.numeric(data.matrix(test.df)[[1]])
[1] TRUE

See  ?integer, ?numeric, ?storage.mode

On Fri, Oct 13, 2017 at 2:07 PM, Ed Siefker <[hidden email]> wrote:

> I have a data frame full of integer values.  I need a matrix full of
> numeric values.
>
> ?data.matrix reads:
>
>      Return the matrix obtained by converting all the variables in a
>      data frame to numeric mode and then binding them together as the
>      columns of a matrix.
>
> This does not work.
>
> test.df <- data.frame(a=as.integer(c(1,2,3)), b=as.integer(c(4,5,6)))
>> class(test.df[[1,1]])
> [1] "integer"
>> class(data.matrix(test.df)[[1]])
> [1] "integer"
>
> What's going on here?
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.