My goal is to import all txt files from a certain folder into a list.
So I do:
setwd(".../folder")
data <- list.files(pattern = "\\.txt$")
lis <- lapply(data, read.csv)
However, I would like to avoid using setwd()
. So I can do:
data <- list.files(path = ".../folder", pattern = "\\.txt$")
But then of course I get an error message No such file or directory as read.csv
looks in the wrong directory. How can I specify the folder in combination with importing all files in data
?
Two quick options for combining text files.Open the two files you want to merge. Select all text (Command+A/Ctrl+A) from one document, then paste it into the new document (Command+V/Ctrl+V). Repeat steps for the second document. This will finish combining the text of both documents into one.
Open the Excel spreadsheet where you want to save the data and click the Data tab. In the Get External Data group, click From Text. Select the TXT or CSV file you want to convert and click Import.
Navigate to the files location and select all text files. Click OK . All text files will be imported to a new spreadsheet. It might take a while to complete, if there are large number of files, so be patient.
Use the full.names
statement in the list.files
.
data <- list.files("../folder", pattern = "\\.txt",full.names = TRUE)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With