0

I cannot seem to convert a list to a matrix. I load a .csv file using:

dat = read.csv("games.csv", header = TRUE)

> typeof(dat)
[1] "list"

But when I try to convert it into a numeric matrix using:

games = data.matrix(dat)

The entries' values are all changed for some reason. What is the problem?

Roy
  • 837
  • 1
  • 9
  • 22
  • 1
    The question is a little unclear due to the lack of information. For example, can you describe the dimensions or structure of dat? Would you be able to show some sample output when you run "head(dat)" in the console, as well as the output you get when you run your games = data.matrix(dat) line (perhaps you can show head(games)? My answer below is a first response based on imperfect information. – Nathaniel Payne Apr 08 '14 at 16:48
  • 1
    You should provide a [**minimal, reproducible example**](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) – Henrik Apr 08 '14 at 16:49

2 Answers2

2

While Nathaniel's solution worked for you, I think it's important to point out that you might need to adjust your perception of what is going on.

The typeof(dat) might be a list but the class is a data.frame.

This might help illustrate the difference:

# typeof() & class() of `pts` is `list`
# whereas typeof() `dat` in your example is `list` but
# class() of `dat` in your example is a `data.frame`

pts <- list(x = cars[,1], y = cars[,2])

as.matrix(pts)
##   [,1]      
## x Numeric,50
#3 y Numeric,50

head(as.matrix(data.frame(pts)))
##      x  y
## [1,] 4  2
## [2,] 4 10
## [3,] 7  4
## [4,] 7 22
## [5,] 8 16
## [6,] 9 10

Those are two substantially different outcomes from the 'as.matrix()` function.

Just making sure you don't get disappointed of the outcome if you try this in a different context outside of read.csv.

hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
1

Without any other information being provided, perhaps you might try:

games <- as.matrix(dat)
Nathaniel Payne
  • 2,749
  • 1
  • 28
  • 32