How to calculate frequency distributions?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to calculate frequency distributions?
# 1  
Old 04-12-2010
How to calculate frequency distributions?

Hello,

I'm trying to get lists of the frequency distributions for each of two variables (vars C and N in the examples). I'd like the distribution for each variable to range from the min of the two variables to the max of the two variables. I can work out the max value beforehand by ordering the data by variable. Not sure how to calculate the frequency distribution though.
Any notes on the functions of different parts of the commands, would be much appreciated too! Still learning...

Thank you!Smilie


Example input file:

Var, Num
C,5
C,0
C,5
C,2
C,0
N,1
N,1
N,0
C,5
C,3

request output file:

Var, Num, Frq
C,0,2
C,1,0
C,2,1
C,3,1
C,4,0
C,5,3
N,0,1
N,1,2
N,2,0
N,3,0
N,4,0
N,5,0

OR,

Num, Frq_C, Frq_N
0,2,1
1,0,2
2,1,0
3,1,0
4,0,0
5,3,0
# 2  
Old 04-12-2010
Since you want zero frequency you have to provide the classes you want to report on - ahead of time. The program cannot know what to expect.
So edit a file, call it "expected"
Code:
C,0
C,1
C,2
C,3
C,4
C,5
N,0
N,1
N,2
N,3
N,4
N,5

Code:
awk 'FILENAME=="expected" {arr[$0]=0}
       FILENAME=="input" {arr[$0]++}
       END {for (i in arr){ print i "," arr[i]}} '  expected input > newfile

# 3  
Old 04-19-2010
Thanks for your help! Sorry for delayed reply.
The real file that I'm working from is pretty big (over 20 million rows) and the max value is about 10,000. Is there a quick way to generate the "expected" file with all values from 0 to 10,000 for each of C and N?

Thanks again!Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Calculate age of a file | calculate time difference

Hello, I'm trying to create a shell script (#!/bin/sh) which should tell me the age of a file in minutes... I have a process, which delivers me all 15 minutes a new file and I want to have a monitoring script, which sends me an email, if the present file is older than 20 minutes. To do... (10 Replies)
Discussion started by: worm
10 Replies

2. UNIX for Dummies Questions & Answers

unix distributions?

I'm new in the UNIX world. I'm just wondering what are the different examples of unix distributions? (2 Replies)
Discussion started by: j3ff_skull
2 Replies

3. Shell Programming and Scripting

/etc/init.d available in all Linux distributions?

Hi All, I would just like to know if the /etc/init.d directory which is used to hold the start up scripts is available in all linux distributions? Are there any exceptions One more question Is the command chkconfig available in all Linux distributions and used in a similar fashions... (2 Replies)
Discussion started by: gurubarancse
2 Replies

4. Shell Programming and Scripting

using perl to calculate frequency and multiply

Suppose u have two input files FILE A and FILE B FILE A AACD ABBD ACBC FILE B s/ A B C D E A 1 -2 3 4 2 B 3 2 -1 2 1 C 2 3 1 2 3 D 3 4 -3 2 2 E 1 3 4 2 3 So in FILE A we have calculated frequency... (3 Replies)
Discussion started by: cdfd123
3 Replies

5. UNIX for Dummies Questions & Answers

Unix or Linux distributions for a PC

Hi all, I'd like to install either Unix or Linux on my PC and I don't know which way to go- I've tried with HP-UX 11.11 and it failed but I've heard that LInux is the fastest growing operating system in the world.I am asking you thus what sort of Linux distribution would be the best choice for a... (8 Replies)
Discussion started by: adrian262
8 Replies

6. Slackware

Packages of different distributions

hi, as you know nearly every distribution has its own package-management and it needs special packages to install different software. For slackware it's *.tgz, for debian *.deb, for many rpm's *.rpm and so on, but I wonder how a package can be built to be compatibel with every maschine. An... (2 Replies)
Discussion started by: avaurus
2 Replies
Login or Register to Ask a Question