Given a tab separated data file (or another separator), how would you compute the mean of a column?

Too bad that there are no simple binaries that performs simple mathematical operations over those kind of files.

link|improve this question

feedback

2 Answers

up vote 7 down vote accepted
awk -F'\t' '{ sum += $1 } END { print sum / NR }'

Here $1 is the first \tab-separated column.

link|improve this answer
feedback

If you have R installed you can also use:

Rscript -e "(mean(read.table(\"file.tab\")))"

You can also change the function (mean) to other statistical functions, say:

Rscript -e "(mean(read.table(\"file.tab\")))"
Rscript -e "(sd(read.table(\"file.tab\")))"
Rscript -e "(summary(read.table(\"file.tab\")))"

If you want to specify the column, say, use column 3, then you use the notation [,3]

Rscript -e "(sum(read.table(\"file\")[,3]))"

Enjoy!

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.