简体   繁体   English

将特定R包中的功能应用于文件夹中的所有文件

[英]Apply a function from a specific R package to all files in folder

I have a large table that I am reading into R as a data frame. 我有一张大桌子,正在读入R作为数据框。 After ordering and subsetting the df I break it into a list using the split() function. 在对df进行排序和子集设置后,我使用split()函数将其分为一个列表。 I then write that list out as individual text files. 然后,我将该列表写为单独的文本文件。 I am now reading the individual .txt files back into R so that I can determine the effectiveSize (from the coda package) of each file that does not lack Energy values. 现在,我将单个.txt文件读回到R中,以便可以确定每个不缺少Energy值的文件的有效大小(来自coda包)。 However I don't think this is the most efficient method. 但是,我认为这不是最有效的方法。 In any case, is there a way that I could apply the effectiveSize to each individual file or element of the list? 无论如何,有没有一种方法可以对每个文件或列表元素应用有效大小? When I test the effectiveSize function on one of the output files - effectiveSize(ASP29A[,3]) it works fine. 当我在其中一个输出文件-effectiveSize(ASP29A [,3])上测试有效大小函数时,它可以正常工作。 But that's only one of 102 files/elements. 但这只是102个文件/元素中的一个。

The original ordered data frame looks like this; 原始的有序数据帧如下所示;

Chain  Res    Energy
 A    ALA28  -1.8046
 A    ALA28  -2.1910
 A    ALA28  -1.8403
 A    ALA28  -2.1813
 A    ALA28  -2.3693
 A    ALA28  -2.2808

I hope this is clear. 我希望这很清楚。

As a list, the data looks like 作为列表,数据看起来像

$ C.017500:'data.frame':    6003 obs. of  3 variables:
..$ Chain : Factor w/ 3 levels "A","B","C": 3 3 3 3 3 3 3 3 3 3 ...
..$ Res   : chr [1:6003] "017500" "017500" "017500" "017500" ...
..$ Energy: num [1:6003] -37 -33.8 -34.7 -35.4 -35 ...

$ A.ALA28 :'data.frame':    6003 obs. of  3 variables:
..$ Chain : Factor w/ 3 levels "A","B","C": 1 1 1 1 1 1 1 1 1 1 ...
..$ Res   : chr [1:6003] "ALA28" "ALA28" "ALA28" "ALA28" ...
..$ Energy: num [1:6003] -1.8 -2.19 -1.84 -2.37 -2.18 ...

And so on through 102 elements. 依此类推,通过102个元素。

Here's how I would do it: 这是我的处理方式:

  • Read the file list using: 使用以下命令读取文件列表:

     setwd("path/to/files/") file.list <- dir(pattern = "txt$") 

    The pattern bit is optional, but it can help you filtering only some files. pattern位是可选的,但是它可以帮助您仅过滤某些文件。

  • Use sapply to run whatever function you want 使用sapply运行所需的任何功能

     res <- sapply(file.list, yourfunction) 

For instance, to know the file size of the files you could use: 例如,要了解文件的文件大小,可以使用:

res <- sapply(file.list, file.size)

You may want to create function to call effectiveSize , such as: 您可能需要创建函数来调用effectiveSize ,例如:

eff.size <- function(filename)
      {
      data <- read.table(filename)
      # <do something here with the data as needed>
      res <- effectiveSize(<appropriate parameters>)

      # return the result
      res
      } 

Then call 然后打电话

 res <- sapply(file.list, eff.size)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM