Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 读取所有列文本_R_Excel_Coercion - Fatal编程技术网

R 读取所有列文本

R 读取所有列文本,r,excel,coercion,R,Excel,Coercion,我有一个Excel文件,所有列都是“text”类型。然而,当调用read\u excel时,一些列被猜测为“dbl”。我知道我可以使用col\u types来指定列,但这需要我知道文件中有多少列 有没有办法检测列数?或者,指定列都是“text”?差不多 read_excel("file.xlsx", col_types = "text") 这相当合理地给出了一个错误,即我没有为所有列指定类型 目前,我可以通过读取文件两次来解决此问题: read_excel_one_type <- fun

我有一个Excel文件,所有列都是“text”类型。然而,当调用
read\u excel
时,一些列被猜测为
“dbl”
。我知道我可以使用
col\u types
来指定列,但这需要我知道文件中有多少列

有没有办法检测列数?或者,指定列都是
“text”
?差不多

read_excel("file.xlsx", col_types = "text")
这相当合理地给出了一个错误,即我没有为所有列指定类型

目前,我可以通过读取文件两次来解决此问题:

read_excel_one_type <- function(filename, col_type = "text"){
  temp <- read_excel(path = filename)
  ncol.temp <- ncol(temp)
  read_excel(path = filename, col_types = rep(col_type, ncol.temp))
}
read\u excel\u one\u type
library(“xlsx”)

file这个答案似乎很有帮助:。我发现excel文件需要从一开始就正确格式化,以便R自动检测正确的数据类型(即数字、日期、文本)。我认为这篇文章与你的问题更相关。海报上显示的代码与您提供的代码类似,只是只读取一行数据以确定列数,然后根据第一行将其余数据读入R。

一种方法是使用
skip
arguments只读取第一行。我想
skip
是指在读取数据之前要跳过多少行,而不是跳过之前要读取多少行?
library("xlsx")
file<-"myfile.xlsx"
sheetIndex<-1
mydf<-read.xlsx(file, sheetIndex, sheetName=NULL, rowIndex=NULL,
      startRow=NULL, endRow=NULL, colIndex=NULL,
      as.data.frame=TRUE, header=TRUE, colClasses=NA,
      keepFormulas=FALSE, encoding="unknown")