Converting from a list to numeric in R

73,221

Here is a shorter/faster way to turn your data.frame into a numeric matrix:

data <- data.matrix(data)

There is also

data <- as.matrix(data)

but one important difference is if your data contains a factor or character column: as.matrix will coerce everything into a character matrix while data.matrix will always return a numeric or integer matrix.

data <- data.frame(
  logical   = as.logical(c(TRUE, FALSE)),
  integer   = as.integer(c(TRUE, FALSE)),
  numeric   = as.numeric(c(TRUE, FALSE)),
  factor    = as.character(c(TRUE, FALSE))
)

data.matrix(data)
#      logical integer numeric factor
# [1,]       1       1       1      2
# [2,]       0       0       0      1

as.matrix(data)
#      logical integer numeric factor 
# [1,] " TRUE" "1"     "1"     "TRUE" 
# [2,] "FALSE" "0"     "0"     "FALSE"
Share:
73,221
Edgardo Ortiz
Author by

Edgardo Ortiz

Updated on July 07, 2020

Comments

  • Edgardo Ortiz
    Edgardo Ortiz almost 4 years

    I recently had a problem in which everytime I read a csv file containing a table with values, R read it as a list format instead of numeric. As no thread provided me the entire answer for my situation, once I was able to make it run I decided to include here the script that worked for me in hope that it is useful to someone. Here it is, with some description and some options in case you need it:

    (1) Read the data from a csv file. Here the file has no header, so I put F, if yours have a header, then change it to T.

    data <- read.csv("folder_path/data_file.csv", header=F)
    

    (1.a) Note: If you get a warning that says "incomplete final line found by readTableHeader", that means that R did not find an end-of-file symbol. Just put an extra empty line at the end in the csv file and the message will not show up again.

    (2) You can check that the data is in list format (if it is numeric, then you are all set and don't need this procedure at all!) with the mode command.

    mode(data)
    

    (3) Initialize a matrix (as NA) where you want the data in numeric format, using the dimensions of data.

    dataNum <- matrix(data = NA, nrow = dim(data)[1], ncol = dim(data)[2])
    

    (4) OPTIONAL: If you want to add names to your columns and/or rows, you could use one if these options.

    (4a) Add names to the columns and rows, assuming that each have similar information, in other words you want the names to be col_1, col_2, ... and row_1, row_2, ...

    colnames(dataNum) <- colnames(dataNum, do.NULL = F, prefix = "col_")
    rownames(dataNum) <- rownames(dataNum, do.NULL = F, prefix = "row_")
    

    (4b) If you want different names for each column and each row, then use this option instead and add all the names by hand.

    colnames(dataNum) <- c("col_name_1", "col_name_2")
    rownames(dataNum) <- c("row_name_1", "row_name_2")
    

    (5) Transform the data from list to numeric form and put it in the matrix dataNum.

    for (i in 1:dim(data)[2]) {
        dataNum[,i] <- c(as.numeric(data[[i]]))
    }
    

    (6) You can check that the matrix is in numeric format with the mode command.

    mode(dataNum)
    

    (7) OPTIONAL: In case you would like to transpose the matrix, you can use the following instruction.

    dataNum <- t(dataNum)
    
  • SiKiHe
    SiKiHe almost 9 years
    Neither approach (original posts or answer below) works for me, so I still can't get the rowSums...
  • flodel
    flodel almost 9 years
    @SikiHe: not a very constructive comment, can you elaborate? Maybe create your own question with a reproducible example and link to it from here...
  • SiKiHe
    SiKiHe almost 9 years
    I have exactly the same problem as above, which is why I thought it wasn't necessary to create my own post. I'm importing a csv-file and trying to get the rowSums, but get an error message that the input to rowSums has to be numeric. I tried both approaches described here and neither one of them worked. That is, either the figures got converted into ranks and so the rowSums were incorrect because it wasn't the original figures that were added, or they were not numerical and couldn't be added in the first place.
  • flodel
    flodel almost 9 years
    Please post your data. You must have some non-numeric somewhere forcing R to treat the whole column(s) as a factor.