从列表转换为R中的数字 [英] Converting from a list to numeric in R

查看:117
本文介绍了从列表转换为R中的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近遇到了一个问题,每次读取包含带有值的表的csv文件时,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)从csv文件读取数据.这里文件没有标题,所以我放F,如果您有标题,则将其更改为T.

(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)注意:如果收到警告说"readTableHeader找到的最后一行不完整",则表示R找不到文件结尾符号.只需在csv文件的末尾添加一个多余的空行,该消息将不会再次显示.

(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)您可以使用mode命令检查数据是否为列表格式(如果为数字,则表示已经全部设置好了,根本不需要此过程!).

(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)使用数据的维数初始化一个要以数字格式存储数据的矩阵(作为NA).

(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)可选:如果要在列和/或行中添加名称,则可以使用其中的一个.

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

(4a)假定每个列和行具有相似的信息,请在这些列和行中添加名称,换句话说,您希望这些名称分别为col_1,col_2,...和row_1,row_2,...

(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)如果要为每列和每行使用不同的名称,请改用此选项并手动添加所有名称.

(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)将列表中的数据转换为数字形式,并将其放入矩阵dataNum中.

(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)您可以使用mode命令检查矩阵是否为数字格式.

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

mode(dataNum)

(7)可选:如果您想转置矩阵,可以使用以下指令.

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

dataNum <- t(dataNum)

推荐答案

以下是将data.frame转换为数字矩阵的更短/更快的方法:

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

data <- data.matrix(data)

还有

data <- as.matrix(data)

但是一个重要的区别是,如果您的数据包含因子或字符列:as.matrix会将所有内容强制转换为字符矩阵,而data.matrix始终返回numericinteger矩阵.

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"

这篇关于从列表转换为R中的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆