read_excel'期待数字'…和值是数字

我没有find这个问题的答案,所以希望这是得到一些帮助的地方。

我正在读取包含在.zip文件中的许多Excel文件。 我所拥有的每个.zip都有大约40个我想要读取的excel文件。 我正在尝试创build数据框的列表,但在根据文件内容读取某些文件时遇到错误。

这是在for循环中读取的语句:

library(readxl) df[[i]] <- read_excel(xls_lst[i], skip = 4, col_names = FALSE, na = "n/a", col_types = data_types) 

data_types具有以下值: > data_types [1] "text" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric"

这个文件是正确的。

read_excel语句适用于某些文件,但会在其他文件上返回警告消息:

 In read_xlsx_(path, sheet, col_names = col_names, col_types = col_types,... : [54, 7]: expecting numeric: got '9999.990000' 

那么,值“9999.99000”看起来像一个数字给我。 当我打开创build此警告的Excel文件时,该文件确实显示了这些值,并且还显示该列在Excel中被格式化为文本。 当我将列格式更改为数字时,重新保存Excel工作表,然后正确读入数据。

但是,我有几百这些文件读取…如何可以read_excel忽略由Excel指示的列格式,而是使用col_type定义,我在调用语句中提供?

谢谢,

我试图build立一个玩具的例子。

我的xlsx文件包含:

 3 1 3 3 4 4 5 5 7 '999 6 3 

以你的方式阅读:

 data_types<-c("numeric","numeric") a<-read_excel("aa.xlsx", col_names = FALSE, na = "n/a", col_types = data_types ) Warning message: In read_xlsx_(path, sheet, col_names = col_names, col_types = col_types, : [5, 2]: expecting numeric: got '999' 

text阅读所有text

 data_types<-c("text","text") dat<-read_excel("aa.xlsx", col_names = FALSE, na = "n/a", col_types = data_types ) 

并使用type.convert

 dat[]<-lapply(dat, type.convert) 

至less为这个简单的例子起作用。

*编辑:

代码中有一个错误。

*编辑回应评论:

另一个玩具示例,演示如何应用type.convert到您的数据:

 #list of data frames l<-list() l[[1]]<-data.frame(matrix(rep(as.character(1:5),2), ncol = 2), stringsAsFactors = FALSE) l<-rep(l,3) #looping over your list to encode columns correctly: for (i in 1: length(l)){ l[[i]][]<-lapply(l[[i]], type.convert) } 

可能有更好的解决scheme。 但我认为这应该工作。

我使用R读取一些date数据,“readxl”是禁用获取all.try为read.csv,这是很好的大数据!

 > library(readxl) > timeline<-read_excel("G:/gua/timelines/time.xlsx") There were 50 or more warnings (use warnings() to see the first 50) > warnings()警告信息: 1: In read_xlsx_(path, sheet, col_names = col_names, col_types = col_types, ... : [146, 81]: expecting numeric: got '2016-09-12 10:20:23' 2: In read_xlsx_(path, sheet, col_names = col_names, col_types = col_types, ... : [146, 82]: expecting numeric: got '2016-09-12 14:23:23'