0
$\begingroup$

I have many text files of tables, 300 rows long and 5 columns across. I can import all the files together as they're all named similarly. I would like to average column 2 of every file together, element by element. Meaning, I want one list of 300 numbers. I keep averaging the whole individual column together, and not the elements from each different column. I might have up to several hundred files, so I'm trying to automate this as much as possible.

$\endgroup$
5
  • 1
    $\begingroup$ Try: (Mean[Import[#,"Data"]][[All,2]])& /@ FileNames[...] $\endgroup$ Commented Apr 3, 2023 at 16:16
  • 1
    $\begingroup$ Consider this contrived example: (t1 = Table[ConstantArray[i, 10], {i, 15}]) // TableForm . Each column comes from one file. Then Mean /@ t1 would give you the mean across each row at a time that (I think) you want. $\endgroup$ Commented Apr 3, 2023 at 16:33
  • $\begingroup$ Amazing, thank you! Is there a way to drop the first row of each file before averaging? Throwing a Drop[...,1] in that command somewhere? $\endgroup$ Commented Apr 3, 2023 at 16:35
  • 1
    $\begingroup$ Mean /@ Rest@t1 will drop the first row. $\endgroup$ Commented Apr 3, 2023 at 16:36
  • 2
    $\begingroup$ try also Mean[Rest@Import[#, {"Data", All, 2}] & /@ files] (where files is your list of file names) $\endgroup$ Commented Apr 3, 2023 at 18:11

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.