Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting from a list to numeric in R

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)
like image 777
Edgardo Ortiz Avatar asked Jul 06 '13 20:07

Edgardo Ortiz


1 Answers

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"
like image 153
flodel Avatar answered Oct 15 '22 13:10

flodel