5

当我从包中的Posti-Letto-Istat.xls文件中读取任何工作表时,我没有任何问题:read_excelreadxl

library(readxl)
pl_istat1 <- read_excel(path = "data/Posti-Letto-Istat.xls", sheet = 1, range = "A6:I66", na = "....")

但是,如果我尝试使用 alapplyfor循环将所有三个工作表都放在一个列表中,则会出现以下错误。

lapply(1:3, function(i) read_excel(path = "data/Posti-Letto-Istat.xls", sheet = i, range = "A6:I66", na = "....")) 

Error in nchar(x, type = "width") : invalid multibyte string, element 4

我看到这是一个编码问题,如果我做类似的事情

names(pl_istat[[i]]) <- iconv(enc2utf8(names(pl_istat[[i]])),sub="byte")

每张纸,然后我没有问题。

但是,有没有办法让列表接受tibble正确导入的readxl

我的会话信息:

R version 3.3.3 (2017-03-06)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United 
States.1252    LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          

[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] readxl_1.0.0

loaded via a namespace (and not attached):
[1] magrittr_1.5     assertthat_0.2.0 R6_2.2.2         tools_3.3.3      bindrcpp_0.2     glue_1.1.1       dplyr_0.7.3      tibble_1.3.4     Rcpp_0.12.12    

[10] cellranger_1.1.0 rematch_1.0.1    pkgconfig_2.0.1  rlang_0.1.2      bindr_0.1     
4

2 回答 2

5

我有同样的错误,可以通过包装解决read_excel()as.data.frame()

lapply(
  1:3, 
  function(i) {
    as.data.frame(
      read_excel(path = "data/Posti-Letto-Istat.xls", sheet = i, range = "A6:I66", na = "....")
    )
  }
) 
于 2018-09-04T14:43:49.360 回答
1

尝试将使用创建的小标题保存readxl到列表中时,我遇到了类似的问题。因为我有多个标题行,所以我首先只读取标题,将它们连接起来并创建一个以headers列名命名的向量。read_excel然后使用和 参数读取实际数据col_names = FALSE。我将这些“无名”小标题保存到列表中没有问题,但是如果我使用重命名列,headers我会收到此错误:

Error in nchar(x[is_na], type = "width") : 
  invalid multibyte string, element 1

我通过在重命名 tibble 之前更改编码来解决它:

headers <- enc2native(headers)

但在此之后,当打印列表时,我收到以下警告:

In fansi::strwrap_ctl(x, width = max(width, 0), indent = indent,  :
  Encountered a C0 control character, see `?unhandled_ctl`; you can use `warn=FALSE` to turn off these warnings.

根据12这似乎是由基础 R 中的错​​误引起的,这对我来说不是问题。

于 2019-08-07T20:14:32.387 回答