Reputation: 358
I am trying to loop paste a directory folder path and some csv file names from this folder below, but getting error. Please help!
#
# Create String of Directory fpath
strdir <- getwd()
vec_files <- dir(path = getwd(), pattern = "*.csv")
# Loop
for (i in 1:length(vec_files)) {
print0(strdir,"/",vec_files[[i]], sep = "")
}
Returns error below:
Error in print.default(strdir, "/", vec_files[[i]], sep = "") :
invalid printing digits -2147483648 In addition: Warning message: In print.default(strdir, "/", vec_files[[i]], sep = "") : NAs introduced by coercion
Upvotes: 0
Views: 437
Reputation: 389175
As mentioned by @teofil you probably want to use paste0
instead of print0
as there is no function named print0
. Moreover, even after using paste0
the loop will not print anything since in for
loop you need to print the objects explicitly so wrap another print
or cat
around paste0
for it to work.
However, you really don't need to do this. dir()
and list.files()
have full.names
parameter which gives the full path of the files if set to TRUE
.
vec_files <- dir(path = getwd(), pattern = "\\.csv$", full.names = TRUE)
Also note that if you are reading from your working directory itself you don't necessarily need the complete path of the file, although it is a good habit to work with complete path always.
Upvotes: 3