2012-04-12 11 views
3

私のコードでは、テキストファイルのフォルダを読み込んでdtmに変換するのが難しいです。問題は何らかの理由で、私のコンピュータが間違いなくディレクトリ内のテキストファイルとの接続しか確立できないということです。返すエラーがされています。しかし、私は簡単に任意のテキストエディタで、だけでなく、Pythonでこれらのファイルを開くことができディレクトリから複数のファイルを読むR

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 

。なぜ私は接続を確立することができ、他のものではないかもしれないという考えはありますか?私のコードは以下の通りです:

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] 
} 

答えて

9

List.filesファイル名だけではなく、フルパスでファイル名を指定します。

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

かだけがfull.names =は 'true'と' list.files'を使用してみてください。 – flodel

+0

よろしくお願いします。ありがとう。これは単なる作業ディレクトリの問題です。時間の無駄に申し訳ありません! – brianabelson

1
directory <- "C://temp" ## for example 
filenames <- list.files(directory, pattern = "*.*", full.names = TRUE) 
+0

私にとって非常に役に立ちます。ありがとうございます+1 –

関連する問題