如何将多个文本文件合并到一个data.frame中? [英] How can I merge multiple text files into one data.frame?

查看:70
本文介绍了如何将多个文本文件合并到一个data.frame中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在同一目录中有9个.txt文件.price1.txt price2.txt price3.txt是来自不同位置的价格表,行和列名分别为经度和海拔.year4,5,6.txt和dis7,8,9.txt是年份和折扣表,其位置与text1中的位置相同.

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.

我想创建一个新的数据框,其中每列都是价格1,年份,折扣文本1-9的列表,以及相应的经度和态度.

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.

我可以使用

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

读取文件名

我能够使用

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

将每个文件放在一起.

但是如何从不同的可变文本文件中提取表并将其放入列中?这里最困难的部分是所有文件名都不同,所以我想不出一种循环文件列表的简单方法.

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.

推荐答案

考虑将价格,年份,光盘文本文件读入其自己的数据框中,然后合并:

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 循环中的通用版本:

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"))

这篇关于如何将多个文本文件合并到一个data.frame中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆