list 在 R 中从列表转换为数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17506691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting from a list to numeric in R
提问by Edgardo Ortiz
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:
我最近遇到了一个问题,每次我读取一个包含带有值的表格的 csv 文件时,R 都会将其读取为列表格式而不是数字格式。由于没有线程为我提供了针对我的情况的完整答案,一旦我能够运行它,我决定在此处包含对我有用的脚本,希望它对某人有用。在这里,有一些描述和一些选项,以防您需要它:
(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.
(1) 从 csv 文件中读取数据。这里文件没有标题,所以我放F,如果你的文件有标题,那就把它改成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.
(1.a) 注意:如果你收到一条警告,说“readTableHeader 找到的最后一行不完整”,这意味着 R 没有找到文件结束符号。只需在 csv 文件的末尾添加一个额外的空行,该消息就不会再次显示。
(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.
(2) 你可以用mode命令检查数据是否是列表格式(如果是数字,那么你都设置好了,根本不需要这个过程!)。
mode(data)
(3) Initialize a matrix (as NA) where you want the data in numeric format, using the dimensions of data.
(3) 使用数据的维度初始化您想要数字格式数据的矩阵(作为 NA)。
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.
(4) 可选:如果您想为列和/或行添加名称,您可以使用这些选项之一。
(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, ...
(4a) 为列和行添加名称,假设每个都有相似的信息,换句话说,您希望名称为 col_1, col_2, ... 和 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.
(4b) 如果您希望每一列和每一行都有不同的名称,请改用此选项并手动添加所有名称。
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.
(5) 将列表中的数据转化为数值形式,放入矩阵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.
(6) 您可以使用mode 命令检查矩阵是否为数字格式。
mode(dataNum)
(7) OPTIONAL: In case you would like to transpose the matrix, you can use the following instruction.
(7) 可选:如果您想转置矩阵,可以使用以下指令。
dataNum <- t(dataNum)
回答by flodel
Here is a shorter/faster way to turn your data.frame into a numeric matrix:
这是将 data.frame 转换为数字矩阵的更短/更快的方法:
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.
但一个重要的区别是您的数据是否包含因子或字符列:as.matrix
将所有内容强制转换为字符矩阵,而data.matrix
始终返回 anumeric
或integer
矩阵。
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"