如何将read.table()多个文件放入R中的单个表中? [英] How to read.table() multiple files into a single table in R?

查看:255
本文介绍了如何将read.table()多个文件放入R中的单个表中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的文件名为< InputData>。< TestName> .csv ,我想为每个测试制作图表。我能看到的最好方法是为每个TestName创建一个R表。每个测试都会产生相同的数据列,因此我想将每个测试的所有数据输入到一个R数据表中,并为输入数据添加一列。

I have filenames named <InputData>.<TestName>.csv and I'd like to make graphs for each test. The best way I can see to do this is to make one R table for each TestName. Each test produces the same columns of data, so I'd like to pull in all the data for each test into an R datatable with an extra column for the inputdata.

我想这样做:

read.tables(c("B217.SE.csv", "C10.SE.csv"), sep=",")

产生(例如):

       Filename  col1   col2
1   B217.SE.csv     1      2
2   B217.SE.csv     2      4
3   C10.SE.csv      3      1
4   C10.SE.csv      4      5

这样做的正确方法是什么?我不知道的一些现有功能?使用for循环用R语言写出来?

What's the right way to do this? Some existing function I don't know about? Writing it out in the R language using a for loop?

推荐答案

我无法对你的数据进行测试,但你会想要使用 apply 类型函数,如下所示:

I can't test it on your data, but you will want to use an apply type function like this:

data <- do.call("rbind", lapply(c("file1", "file2"), function(fn) 
           data.frame(Filename=fn, read.csv(fn)
))

或者,您可以使用 plyr 。这是一个粗略的模拟,它将如何工作(使用数据框而不是文件):

Or, you can simplify it by using plyr. Here's a rough simulation of how that would work (using data frames instead of files):

> df1 <- data.frame(c1=1:5, c2=rnorm(5))
> df2 <- data.frame(c1=3:7, c2=rnorm(5))

在这种情况下,我将使用 get 而不是 read.csv

In this case I will use get instead of read.csv:

> data <- ldply(c("df1", "df2"), function(dn) data.frame(Filename=dn, get(dn)))
> data
  Filename c1          c2
1  df1  1 -0.15679732
2  df1  2 -0.19392102
3  df1  3  0.01369413
4  df1  4 -0.73942829
5  df1  5 -1.27522427
6  df2  3 -0.33944114
7  df2  4 -0.12509065
8  df2  5  0.11225053
9  df2  6  0.88460684
10 df2  7 -0.70710520

编辑

根据Marek的建议,您可以覆盖或创建自己的功能:

Taking Marek's suggestion, you can either overwrite or create your own function:

read.tables <- function(file.names, ...) {
    require(plyr)
    ldply(file.names, function(fn) data.frame(Filename=fn, read.csv(fn, ...)))
}

data <- read.tables(c("filename1.csv", "filename2.csv"))

这篇关于如何将read.table()多个文件放入R中的单个表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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