Excel中的数字列转换为逻辑

时间:2021-01-10 15:32:28

When I try to import data into R studio from Excel, the number columns are getting imported as Logical in R.

当我尝试从Excel将数据导入R studio时,数字列将在R中导入为Logical。

Is there a way to import these columns as a number into R? The column in Excel has been formatted to number.

有没有办法将这些列作为数字导入R? Excel中的列已格式化为数字。

I am using read_excel to import the file.

我正在使用read_excel导入文件。

The spreadsheet I am trying to import has 80 columns.

我尝试导入的电子表格有80列。

2 个解决方案

#1


3  

use col_types and explicitly specify the column types.

使用col_types并显式指定列类型。

read_excel(path, sheet = 1, col_names = TRUE, col_types = c("text","numeric","date"), na = "", skip = 0)

https://www.rdocumentation.org/packages/readxl/versions/0.1.1/topics/read_excel

#2


0  

As you did not provide a dataset as an example, I came up with the following dataset:

由于您没有提供数据集作为示例,我想出了以下数据集:

df <- structure(list(`1_a` = c(1212, 1221, 32432), `2_a` = c(121, 123, 3), `3_a` = c(34, 343, 232), 
                 `4_a` = c(65, 23, 123), `5_a` = c(34, 432, 1)), row.names = c(NA, -3L), 
            class = c("tbl_df", "tbl", "data.frame"))

The dataset are all numeric, with column names starting with a number.

数据集都是数字,列名以数字开头。

Using the following code, I am able to read the excel file while retaining the column names as they are (test.xlsx being an example of the above dataset):

使用以下代码,我能够在保留列名的同时读取excel文件(test.xlsx是上述数据集的一个示例):

library(readxl)
df <- read_excel("test.xlsx", sheet = 1, col_names = TRUE)

#1


3  

use col_types and explicitly specify the column types.

使用col_types并显式指定列类型。

read_excel(path, sheet = 1, col_names = TRUE, col_types = c("text","numeric","date"), na = "", skip = 0)

https://www.rdocumentation.org/packages/readxl/versions/0.1.1/topics/read_excel

#2


0  

As you did not provide a dataset as an example, I came up with the following dataset:

由于您没有提供数据集作为示例,我想出了以下数据集:

df <- structure(list(`1_a` = c(1212, 1221, 32432), `2_a` = c(121, 123, 3), `3_a` = c(34, 343, 232), 
                 `4_a` = c(65, 23, 123), `5_a` = c(34, 432, 1)), row.names = c(NA, -3L), 
            class = c("tbl_df", "tbl", "data.frame"))

The dataset are all numeric, with column names starting with a number.

数据集都是数字,列名以数字开头。

Using the following code, I am able to read the excel file while retaining the column names as they are (test.xlsx being an example of the above dataset):

使用以下代码,我能够在保留列名的同时读取excel文件(test.xlsx是上述数据集的一个示例):

library(readxl)
df <- read_excel("test.xlsx", sheet = 1, col_names = TRUE)