导入多个文件并为其编制索引 [英] Importing several files and indexing them

查看:107
本文介绍了导入多个文件并为其编制索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有n个文件,file_1,file_2,...,file_n,我想导入和使用它们。所以我导入这样的文件

I have n files, file_1, file_2,..., file_n that I want to import and work with. So I import the files like this

files <- list.files(pattern=".txt$") 
for(i in files) { 
    x <- read.table(i, header=TRUE, comment.char = "A", sep="\t")
    assign(i, x)
}

重点是我想同时使用不同文件中的数据。例如,我想计算每个文件的第一列的均值向量:

The point is that I want to use data simultaneously from the different files. For example I want to compute the means vector of the first column of each file:

 meanv = mean(file_1$firstcolumn, file_2$firstcolumn, ..., file_n$firstcolumn).

执行此操作的最合理方法是写入遍历所有文件的循环( file_1 file_2 ,..., file_n )。在这种情况下,您需要索引文件。这有什么解决方案吗?还是有其他解决方案。

the most logical way to do this is the write a loop going through all files (file_1, file_2,..., file_n). In this case you need to index the files. Is there any solution to this? Or is there any other solution.

推荐答案

只需使用清单:

##Generate some test data
R> dd1 = data.frame(V1 = rnorm(10), V2 = rnorm(10))
R> dd2 = data.frame(V1 = rnorm(10), V2 = rnorm(10))
#Create an empty list
R> l = list()
##In your example, you would have something like:
##l[[i]] = read.table(....)
R> l[[1]] = dd1; l[[2]] = dd2

##Now use lapply to calculate the column means for each data frame
R> lapply(l, colMeans)
[[1]]
     V1      V2 
-0.6805 -0.0767 

[[2]]
      V1       V2 
0.253563 0.006207 

这篇关于导入多个文件并为其编制索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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