Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing several txt files from certain folder

Tags:

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?

like image 710
erc Avatar asked Jun 14 '16 06:06

erc


People also ask

How do I add multiple text files?

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.

How do I extract Data from multiple text files in Excel?

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.

How do I import multiple textbooks into Excel 2013?

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.


1 Answers

Use the full.names statement in the list.files.

data <- list.files("../folder", pattern = "\\.txt",full.names = TRUE)
like image 54
Therkel Avatar answered Sep 28 '22 02:09

Therkel