loops - run script multiple times on .txt files and save each output in single table in r -


i have run correlation analysis on over 100 .txt files. have script reads single file, organizes data in appropriate way need, , stores correlation value new variable. script quite large data gets reformatted lot.

my question. how can make script run repeatedly on 100+ .txt files, , stores single correlation value 100+ in single df? ideally final df consist of 2 columns, 1 .txt id , correlation coefficient, , have 100+ rows.

can literally copy , paste script loop? if how appear? i'm newbee! ideas? thanks!

as akrun mentioned, can lapply. without seeing data, recommend this:

my.files <- list.files(pattern = "txt")  # use pattern matches files want read in output <- lapply(my.files, correlation_function)  # combine list of outputs single data.frame output.df <- do.call(rbind, output) 

this assumes have function called correlation_function takes filename input, loads file r, runs correlation analysis, , returns data.frame.


Comments