s-news
[Top] [All Lists]

transpose rows to columns change in data format (Summary of advice recei

To: s-news@lists.biostat.wustl.edu
Subject: transpose rows to columns change in data format (Summary of advice received)
From: "dorothy pang" <dorothy44@hotmail.com>
Date: Wed, 17 Nov 2004 00:53:19 +0000
Dear all

I have received advice from Sundar and Petr on my queries, thanks a lot to both of you.

The advice is summarized as follows

My query:
I have a dataset which has the following format, and I would like to transpose records of patients having more than one diagnosis type from rows having a number of diagnosis into columns of diagnosis type in S-plus version 6.

A complication is that I don't know how many diagnosis types in the data (which is very huge) and new data which comes in may have a greater number of diagnosis type which I need to convert into columns, hence I hope to get a generic programming to do this.

Format of data x:
Name        Diagnosis    Type
Ang Poh Oh    2506        diag1
Ang Poh Oh    4652        diag2
Ang Poh Oh    2510        diag3
Ang Poh Oh    2516        diag4
Toh Yu Mei    4502        diag1
Owen Low    4560        diag1
Owen Low    4565        diag2
Owen Low    8460        diag3

Format of data which I am trying to obtain:

Name        Type1        Type2        Type3        Type4
Ang Poh Oh    2506        4652        2510        2516
Toh Yu Mei         4502
Owen Low                    4560        4565        8460

Solution
> reShape(x$Diagnosis, id = x$Name, colvar = x$Type)
           diag1 diag2 diag3 diag4
Ang Poh Oh  2506  4652  2510  2516
  Owen Low  4560  4565  8460    NA
Toh Yu Mei  4502    NA    NA    NA


_________________________________________________________________
Take a break! Find destinations on MSN Travel. http://www.msn.com.sg/travel/


<Prev in Thread] Current Thread [Next in Thread>
  • transpose rows to columns change in data format (Summary of advice received), dorothy pang <=