user3697665
user3697665

Reputation: 307

How can I merge multiple text files into one data.frame?

Suppose I have 9 .txt files in the same directory. price1.txt price2.txt price3.txt is a table of price from different location, row and col name is longitude and altitude correspondingly. year4,5,6.txt and dis7,8,9.txt is a table of year and discount from the same location as in text1.

I want to create a new data frame where each column is a list of a price, year, discount from text 1-9 with the corresponding longitude and attitude.

I was able to use

mydata = list.files(pattern = "\\.txt$") 

to read the names of the files

I was able to use

a = lapply(mydata, read.table, header = TRUE) 

to have put together each files.

But how can I extract the table from different variable text files and put them into a column? The difficult part here is that all file names are different so I cant figure out a simple way to loop the list of files.

Upvotes: 0

Views: 8887

Answers (1)

Parfait
Parfait

Reputation: 107587

Consider reading in price, year, disc text files into their own dataframes then merging:

pricelist <- list.files(pattern = "price-.*\\.txt")
pricedf <- lapply(pricelist, read.table, header=TRUE)

yearlist <- list.files(pattern = "year-.*\\.txt")
yeardf <- lapply(yearlist, read.table, header=TRUE)

disclist <- list.files(pattern = "disc-.*\\.txt")
discdf <- lapply(disclist, read.table, header=TRUE)

finaldf <- merge(pricedf, yeardf, by=c("longitude", "altitude"))
finaldf <- merge(finaldf, discdf, by=c("longitude", "altitude"))

For a generalized version in a for loop:

items <- c("price", "year", "disc")

for (item in items) {
   assign(paste0(item, "list"), list.files(pattern=paste0(item, "-.*\\.txt")))
   assign(paste0(item, "df"), lapply(get(paste0(item, "list")), read.table, header=TRUE))
}

finaldf <- merge(pricedf, yeardf, by=c("longitude", "altitude"))
finaldf <- merge(finaldf, discdf, by=c("longitude", "altitude"))

Upvotes: 1

Related Questions