Apply a function from a specific R package to all files in folder -


i have large table reading r data frame. after ordering , subsetting df break list using split() function. write list out individual text files. reading individual .txt files r can determine effectivesize (from coda package) of each file not lack energy values. don't think efficient method. in case, there way apply effectivesize each individual file or element of list? when test effectivesize function on 1 of output files - effectivesize(asp29a[,3]) works fine. that's 1 of 102 files/elements.

the original ordered data frame looks this;

chain  res    energy     ala28  -1.8046     ala28  -2.1910     ala28  -1.8403     ala28  -2.1813     ala28  -2.3693     ala28  -2.2808 

i hope clear.

as list, 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 on through 102 elements.

here's how it:

  • read file list using:

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

    the pattern bit optional, can filtering files.

  • use sapply run whatever function want

    res <- sapply(file.list, yourfunction) 

for instance, know file size of files use:

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

you may want create function call effectivesize, such as:

eff.size <- function(filename)       {       data <- read.table(filename)       # <do here data needed>       res <- effectivesize(<appropriate parameters>)        # return result       res       }  

then call

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

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -