creating a new variable from existing data


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers creating a new variable from existing data
# 1  
Old 12-01-2011
creating a new variable from existing data

Hello,
I have the following data set:
TRAIT DOSE
40 0.4
30 0.3
95 1.2
120 1.7
85 1.4
136 1.8
134 1.8
40 0.4
30 0.3
95 1.2
120 1.7
85 1.4
136 1.8
134 1.8
40 0.4
30 0.3
95 1.2

I want to code a new variable this way. For the Dosage column, say I call it D_GROUP, I would like to group the dosages into 3 distinct groups, that is to say,
Dosage Levels will be
Group1 : 0.0-0.49 including all values more than 0 but less than 0.5
Group2: 0.5-1.49: including all values equal to or more than 0.5 but less than 1.5
Group3: 1.5-2: including all values equal to or more than 0.5 but less than 2

Essentially, I would like my final file to look like this:
TRAIT DOSAGE GROUP
40 0.4 0.0-0.49
30 0.3 0.0-0.49
95 1.2 0.5-1.49
120 1.7 1.5-2
85 1.4 0.5-1.49
136 1.8 1.5-2
134 1.8 1.5-2
40 0.4 0.0-0.49
30 0.3 0.0-0.49
95 1.2 0.5-1.49
120 1.7 1.5-2
85 1.4 0.5-1.49
136 1.8 1.5-2
134 1.8 1.5-2
40 0.4 0.0-0.49
30 0.3 0.0-0.49
95 1.2 0.5-1.49


How do I do this? Please help!
Thank you!
# 2  
Old 12-01-2011
You can try using awk, store the values in an array on your first entry of the file, then read the file again and print the values. Not complete but will give you an idea.

Outputting the results of each range to a separate file and then joining them together will be more efficient.

Code:
BEGIN {
  ARGV[ARGC] = ARGV[ARGC-1]                                    # Read file twice
  ARGC++
  i=1
  j=1
}

FNR == NR {

  if ($2 >= 0.0 || $2 <= 0.49) {
    range1[i] = $0
  }

  if ($2 >= 0.5 || $2 <= 1.49) {
    range1[j] = $0
  }

  i = i++
  j = j++

  next

}

FNR > NR {
  print range1[m] 
}

# 3  
Old 12-01-2011
Code:
$ cat grp.awk
BEGIN { split("0.0:0.5:1.5:2", A, ":"); }

{       for(N=1; A[N+1]; N++)
        if(($2 >= A[N])&&($2 < A[N+1]))
        {
                MAX=A[N+1];
                if(A[N+2]) MAX-=0.01;
                $3=sprintf("%.2f-%.2f", A[N], MAX);
                break;
        }
} 1

$ awk -f grp.awk data

40 0.4 0.00-0.49
30 0.3 0.00-0.49
95 1.2 0.50-1.49
120 1.7 1.50-2.00
85 1.4 0.50-1.49
136 1.8 1.50-2.00
134 1.8 1.50-2.00
40 0.4 0.00-0.49
30 0.3 0.00-0.49
95 1.2 0.50-1.49
120 1.7 1.50-2.00
85 1.4 0.50-1.49
136 1.8 1.50-2.00
134 1.8 1.50-2.00
40 0.4 0.00-0.49
30 0.3 0.00-0.49
95 1.2 0.50-1.49

$

 
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Creating the script for updating or replacing the existing http.conf file

Hi I need some help with a task, i am an absolute newbie to any form of shell scripting and request guidance. I have been building a proxy server using the apache mod proxy currently my solution is working , but i need to automate the process , suppose if any changes need to be made on... (0 Replies)
Discussion started by: satej
0 Replies

2. AIX

Creating NIM SPOT using an existing mksysb from cliet

Hi, i backed up a test LPAR using mksyb utility on AIX. and transferred it to NIM server via NFS. I want to create a SPOT using this mksysb . Please explain me the procedure. And got something online, but that procedure is different. Please respond. (5 Replies)
Discussion started by: System Admin 77
5 Replies

3. Shell Programming and Scripting

Generate tabular data based on a column value from an existing data file

Hi, I have a data file with : 01/28/2012,1,1,98995 01/28/2012,1,2,7195 01/29/2012,1,1,98995 01/29/2012,1,2,7195 01/30/2012,1,1,98896 01/30/2012,1,2,7083 01/31/2012,1,1,98896 01/31/2012,1,2,7083 02/01/2012,1,1,98896 02/01/2012,1,2,7083 02/02/2012,1,1,98899 02/02/2012,1,2,7083 I... (1 Reply)
Discussion started by: himanish
1 Replies

4. Shell Programming and Scripting

add more data to existing data in a file

Hi all, I need help to add additional data from file2 to existing data in file 1 using awk, sed or perl. the ID in file 1 should match against field $3 in file2 file1 #this is a new game ID HR_1 BASE1 30 BASE2 37 DETAIL No TYPE L @@ ID HR_10 BASE1 6030 BASE2 ... (4 Replies)
Discussion started by: redse171
4 Replies

5. Shell Programming and Scripting

Creating/ammending Name Column in existing .txt file

With the help of this forum, I have a script with the following output: chr7 27104483 27105154 chr7 27106872 27110789 chr7 27111956 27112830 chr7 27114388 27125180 chr7 27126966 27131260 chr7 27135440 27137796 which was created by the following script: awk '1 == NR || $NF >= 1000 {... (6 Replies)
Discussion started by: awknerd
6 Replies

6. Filesystems, Disks and Memory

Creating a Mirror RAID With Existing Disk

Hi there, I'm not sure if this is possible, but here is what I'd like to do.. I have an existing 160GB drive in my Redhat 9.0 server that I would like to add an additional 160GB drive to and create a mirrored RAID of the first disk to the new disk. I would like to do this without having to... (2 Replies)
Discussion started by: sysera
2 Replies

7. UNIX for Dummies Questions & Answers

help on appending data to existing data

I need to know how to record the hostname, date/time and all of the process and send it all to one file. I know that the commands I need are hostname, date and ps but I don't know how to do them all and send them all to the same file. Please help! (1 Reply)
Discussion started by: precious51980
1 Replies
Login or Register to Ask a Question