Reading files from a folder and despite indexing c

2019-08-25 16:56发布

I am trying to read files from a folder. I set the working directory and the target folder.

setwd("F://directory/SimulationOutputs/folder - Copy/")
spwfolder <- ("F:/directory/SimulationOutputs/folder - Copy/spws/")
spws <- list.files(spwfolder)

After this step, I check to see if it is getting it right:

 spws[1]
[1] "Cambridge2018_136.txt"

Next, I want to read the first file:

spw<-read.table(spws[1], sep=";", row.names=NULL, skip = 8, fill = TRUE, 
header=TRUE, stringsAsFactors = FALSE)

And I get this message:

Error in file(file, "rt") : cannot open the connection In addition: Warning message: In file(file, "rt") : cannot open file 'Cambridge2018_136.txt': No such file or directory

What am I doing wrong? Thanks in advance!

1条回答
2楼-- · 2019-08-25 17:18

You set the working directory to /folder - Copy/ but your files are in /folder - Copy/spws/.

Alternately, you could use list.files with full.names=TRUE so that spws will contain the full path to the file and then it wouldn't matter where your working directory is set.

查看更多
登录 后发表回答