Reputation: 2763
I have to run a correlation analysis on over 100 .txt files. I have a script which reads a single file, organizes the data in the appropriate way that I need, and then stores the correlation value as a new variable. The script is quite large as the data gets reformatted a lot.
my question. How can I make this script run repeatedly on all the 100+ .txt files, and stores the single correlation value for all 100+ in a single DF? Ideally the final DF would consist of two columns, one with the .txt ID and another with the Correlation coefficient, and it would have 100+ rows.
Can I literally copy and paste the script into a for loop? If so how would that appear? I'm a newbee! Any ideas? Thanks!
Upvotes: 0
Views: 158
Reputation: 3310
As akrun mentioned, you can do this with lapply
. Without seeing your data, I would recommend something like this:
my.files <- list.files(pattern = "txt") # use a pattern that only matches the files you want to read in
output <- lapply(my.files, correlation_function)
# Combine list of outputs into a single data.frame
output.df <- do.call(rbind, output)
This assumes that you have a function called correlation_function
that takes a filename as input, load
s the file into R
, runs the correlation analysis, and returns a data.frame
.
Upvotes: 2