从多个文件中拾取一列,并合并到R中的一个矩阵 [英] Pick up one column from many files and merge to one matrix in R

查看:166
本文介绍了从多个文件中拾取一列,并合并到R中的一个矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多数据文件(* .dat)与列(所有文件格式相同),我想要拾取每个文件的一列(所有文件中的相同位置),并将它们合并到一个数据帧。



你能告诉我可以在R吗?而且如何?






我有大约200个文件(23x7)。这是我的数据文件:

  12000.0000 77.17 59.09 0.17 1.82 59.09 32.4564 
6000.0000 77.52 32.68 0.11 1.83 32.68 17.8731
3000.0000 77.80 18.98 0.12 1.83 18.98 10.3449
1500.0000 77.99 11.23 0.12 1.84 11.23 6.1084
750.0000 78.13 6.93 0.15 1.84 6.93 3.7636
375.0000 78.21 4.53 0.28 1.84 4.53 2.4552
187.5000 78.27 3.37 0.51 1.85 3.37 1.8253
93.7500 78.36 2.84 0.99 1.85 2.84 1.5387
46.8750 78.48 2.04 1.37 1.85 2.04 1.1049
23.4375 78.53 0.98 0.17 1.85 0.98 0.5291
11.7188 78.52 -0.23 0.15 1.85 -0.23 -0.1242
5.8594 78.48 -0.74 0.08 1.85 -0.74 -0.3973
2.9297 78.44 -0.83 0.0 3 1.85 -0.83 -0.4499
1.4648 78.43 -1.49 0.06 1.85 -1.49 -0.8059
0.7324 78.40 -3.20 0.15 1.85 -3.20 -1.7297
0.3662 78.24 -5.33 0.04 1.85 -5.33 -2.8879
0.1831 77.94 -6.84 0.07 1.84 -6.84 -3.7212
0.0916 77.71 -5.76 0.08 1.83 -5.76 -3.1449
0.0458 77.35 -3.57 0.11 1.82 -3.57 -1.9588
0.0229 77.44 -0.88 0.13 1.83 -0.88 -0.4810
0.0114 77.31 0.72 0.23 1.82 0.72 0.3928
0.0057 77.59 1.63 0.51 1.83 1.63 0.8929
0.0029 77.61 0.34 2.65 1.83 0.34 0.1841
pre>

我想要列第6列,并结合其他文件的第6列,以创建一个矩阵(23x200)。

解决方案

这样做的另一种方法(基于@ mdsumner的答案)将是(未经测试):

 #获取文件列表
my.file .list< - list.files(pattern =dat $)
#为每个文件,运行read.table并仅选择第一列
my.list< - lapply(X = my .file.list,FUN = function(x){
read.table(x,colClasses = c(NULL,NULL,numeric,NULL),sep =,) ,1]
})
#将列表中的列合并成一个data.frame
my.df< - do.call(cbind,my.list)


I have many data file (*.dat) with columns (all files in the same format), I want to pick up one column (the same position in all files) of each file and merge them in to a data frame.

Could you tell me is it possible in R? And how?


I have about 200 files (23x7). Here is my data file:

  12000.0000       77.17   59.09    0.17        1.82   59.09   32.4564
   6000.0000       77.52   32.68    0.11        1.83   32.68   17.8731
   3000.0000       77.80   18.98    0.12        1.83   18.98   10.3449
   1500.0000       77.99   11.23    0.12        1.84   11.23    6.1084
    750.0000       78.13    6.93    0.15        1.84    6.93    3.7636
    375.0000       78.21    4.53    0.28        1.84    4.53    2.4552
    187.5000       78.27    3.37    0.51        1.85    3.37    1.8253
     93.7500       78.36    2.84    0.99        1.85    2.84    1.5387
     46.8750       78.48    2.04    1.37        1.85    2.04    1.1049
     23.4375       78.53    0.98    0.17        1.85    0.98    0.5291
     11.7188       78.52   -0.23    0.15        1.85   -0.23   -0.1242
      5.8594       78.48   -0.74    0.08        1.85   -0.74   -0.3973
      2.9297       78.44   -0.83    0.03        1.85   -0.83   -0.4499
      1.4648       78.43   -1.49    0.06        1.85   -1.49   -0.8059
      0.7324       78.40   -3.20    0.15        1.85   -3.20   -1.7297
      0.3662       78.24   -5.33    0.04        1.85   -5.33   -2.8879
      0.1831       77.94   -6.84    0.07        1.84   -6.84   -3.7212
      0.0916       77.71   -5.76    0.08        1.83   -5.76   -3.1449
      0.0458       77.35   -3.57    0.11        1.82   -3.57   -1.9588
      0.0229       77.44   -0.88    0.13        1.83   -0.88   -0.4810
      0.0114       77.31    0.72    0.23        1.82    0.72    0.3928
      0.0057       77.59    1.63    0.51        1.83    1.63    0.8929
      0.0029       77.61    0.34    2.65        1.83    0.34    0.1841

I want to take column 6 and combine with 6th columm from other files to make a matrix (23x200).

解决方案

Another way of doing this (based on @mdsumner's answer) would be (untested):

# get a list of files
my.file.list <- list.files(pattern = "dat$")
# for each file, run read.table and select only the first column
my.list <- lapply(X = my.file.list, FUN = function(x) {
            read.table(x, colClasses = c("NULL", "NULL", "numeric", "NULL"), sep = ",")[,1]
        })
# merge columns that are in a list into one data.frame
my.df <- do.call("cbind", my.list)

这篇关于从多个文件中拾取一列,并合并到R中的一个矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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