gpt4 book ai didi

从目录中读取多个文件,R

转载 作者:行者123 更新时间:2023-12-04 09:37:44 25 4
gpt4 key购买 nike

我的代码中的一个步骤难以读取文本文件文件夹并将其转换为 dtm。问题是,由于某种原因,我的计算机只能间歇性地与目录中的文本文件建立连接。返回的错误是:

Error in file(con, "r") : cannot open the connection
In addition: Warning message:
In file(con, "r") :
cannot open file '[file name]': No such file or directory

但是,我可以在任何文本编辑器以及 python 中轻松打开这些文件。任何想法为什么我有时能够建立连接而不是其他人?我的代码如下:

files <- as.character(list.files(path="[file path]"))
readLines(files[1]) #here is where the error occurs, for example
n <- length(files) #this is my loop
subtitles <- character(n)
subtitle <- character(1)

for (i in 1:n){
subtitle <- as.character(readLines(files[i]))
subtitle <- iconv(subtitle, to="UTF8")
subtitle <- tolower(subtitle)
subtitle <- as.character(paste(subtitle, collapse=" "))
subtitles[i] <- subtitle[1]
}

最佳答案

List.files只给你文件名,不给你完整路径的文件名。试试

files <- as.character(list.files(path="[file path]"))
readLines(paste("[file path]",.Platform$file.sep,files[1],sep=""))

关于从目录中读取多个文件,R,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10117042/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com