简体   繁体   English

从文件导入文本时出错(file.info(filename[i]) 中的 readChar 错误:文件名参数无效)

[英]Error when importing text from files (readChar Error in file.info(filename[i]) : invalid filename argument)

I was importing text from files following the code explained here by Tommy:我按照 Tommy 在此处解释的代码从文件中导入文本:

Import text file as single character string 将文本文件作为单字符串导入

I imported the name of the files (which are in subfolders):我导入了文件的名称(位于子文件夹中):

mydata <- as.data.frame(list.files(path="FolderWithFiles",
                                     full.names = FALSE, recursive = 
TRUE, ignore.case= TRUE, include.dirs = TRUE))

Then I was using a loop to import the content for all the files (the working directory is set to "FolderWithFiles"):然后我使用循环导入所有文件的内容(工作目录设置为“FolderWithFiles”):

filename <- mydata$filename

x<-(1:245)
y<-c(1:245)

for ( i in x) {

  y[i]  <- readChar(filename[i] , file.info(filename[i])$size)

}

This used to work until I changed some of the files.这曾经有效,直到我更改了一些文件。 Now I keep getting this error and I cannot solve it:现在我不断收到这个错误,我无法解决它:

Error in file.info(filename[i]) : invalid filename argument  

I found a solution, I post it for anyone experiencing the same error:我找到了一个解决方案,我将其发布给遇到相同错误的任何人:

Error in file.info(filename[i]) : invalid filename argument  

For some reason after updating R, it started importing the names of files as factors rather than characters.出于某种原因,在更新 R 后,它开始将文件名作为因子而不是字符导入。 It was enough to add:添加以下内容就足够了:

mydata$filename <- as.character(mydata$filename)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM