I have a directory on my computer filled with ~ 1000 .txt files. Each file looks like this (no NAs):
`head` 1.txt
M40_A M40_B M40_C M41
K00844 28 20 27 23
K00845 668 649 737 838
K01810 2171 2264 2140 2221
`head` 2.txt
M40_A M40_B M40_C M41
K00844 23 21 22 11
K00845 649 628 708 837
K01810 2121 2326 2162 2255
For each file, all row names and column names are the same. What I am looking to do is the following: create 1 final data frame in which I have the average (rounded up) computed for each K00XXX number (from all .txt files) for each condition (M40_A, M40_B, M40C, M41). For example, my final df would look like this:
`final_df`
M40_A M40_B M40_C M41
K00844 26 21 25 17
K00845 659 639 723 838
K01810 2146 2295 2151 2238
Where, for example, the value 26 is the average of column M40_A, row K00844 of 1.txt and 2.txt ((28 + 23)/2 = 26). I have searched this site and have found the exact same post here: Average multiple csv files into 1 averaged file in r however, multiple attempts of me trying to execute the code keeps giving me errors. For example:
`txts <- lapply(list.files(pattern="*.txt"), read.csv)'
Reads all of my files into a list, into a weird configuration. This is my result:
`> txts[1]`
[[1] M40_A.M40_B.M40_C.M41
1 K00844\t28\t20\t27\t23\
2 K00845\t668\t649\t737\t838\
3 K01810\t2171\t2264\t2140\t2221\
and when I execute the second code:
`Reduce("+", txts) / length(txts)
it gives me: Warning message:
In Ops.factor(left, right) : ‘+’ not meaningful for factors.
Not to mention, this is not taking the average of all the .txts files as the R documentation says that Reduce
is to combine the elements of a given vector.
So, I think there has to be a different way to be able to make this work. Any help or insight into how to come up with my final_df
would really help so much!