[R] ordering columns in a data frame

Newbie19_02 nvanzuydam at gmail.com
Wed Mar 10 13:16:09 CET 2010


Dear R users,

I have the following data frame:

     PROCHI            date_admission
2   CAO0000713     1999-12-11
4   CAO0000713     1999-10-25
21  CAO0001743     1989-05-04
25  CAO0001743     1996-09-12
26  CAO0001743     1989-05-17
27  CAO0001743     1987-09-17
28  CAO0001743     1987-09-19
29  CAO0001743     1988-01-27
36  CAO0001747     2004-03-21
38  CAO0001747     2004-03-22
39  CAO0001747     2001-02-24
41  CAO0001747     2006-03-31
46  CAO0001901     2007-04-15
61  CAO0002736     2006-05-03
62  CAO0002736     2006-04-05
64  CAO0002736     2006-06-09
65  CAO0002736     2006-04-10
68  CAO0002736     2006-03-14
69  CAO0002736     1995-04-14
72  CAO0002736     1993-11-22
74  CAO0002736     2000-09-26
75  CAO0002736     1995-11-28
77  CAO0003129     1997-05-02
81  CAO0003129     2004-05-08
83  CAO0003129     1998-04-21
85  CAO0003129     1997-04-30
106 CAO0004563     1984-06-10
113 CAO0004563     1984-01-17
153 CAO0012804     1987-02-18
188 CAO0018045     1996-12-04
189 CAO0018045     1996-09-28
291 CAO0030632     1995-04-08
292 CAO0030632     2000-02-10
305 CAO0030632     2007-06-30
306 CAO0030632     2000-07-30
320 CAO0031775     1999-03-05
322 CAO0031775     1992-09-09
323 CAO0031775     1996-07-12
324 CAO0031775     2004-09-12
325 CAO0031775     1998-01-17
328 CAO0031775     1999-02-25
329 CAO0031775     1999-02-26
330 CAO0031775     1999-02-27
331 CAO0031775     2006-03-29
377 CAO0034275     1990-10-05
382 CAO0039712     1982-11-01
387 CAO0039712     2003-01-10
389 CAO0039712     1982-07-13
390 CAO0039712     1996-05-14
393 CAO0039712     1982-07-15
396 CAO0039712     1982-06-20
397 CAO0039712     1982-06-22
399 CAO0039712     1996-07-24
400 CAO0039712     1996-01-26
401 CAO0039712     1996-04-29
427 CAO0045125     1999-02-02
429 CAO0045125     2000-11-04
430 CAO0045125     1998-06-05
431 CAO0045125     1998-06-07
433 CAO0045125     1989-10-09
434 CAO0045125     1989-10-10
435 CAO0045125     1989-09-11
437 CAO0045125     1989-09-13
441 CAO0045125     1999-03-22
444 CAO0045125     1989-09-29
445 CAO0045125     2006-01-31
452 CAO0048575     2006-01-02
453 CAO0048575     2006-01-04
454 CAO0048856     1994-10-06
456 CAO0048856     1994-10-12
461 CAO0048856     1994-03-28
464 CAO0050041     2007-03-01
466 CAO0050041     2007-04-04
481 CAO0050041     2007-07-10
490 CAO0050041     1996-07-16
510 CAO0050041     2003-11-25
514 CAO0050041     2004-05-26
520 CAO0050041     2006-10-29
531 CAO0055131     1993-04-03
533 CAO0055131     1994-07-22
535 CAO0055131     1994-01-31
538 CAO0056636     1997-12-02
550 CAO0059403     1985-12-08
552 CAO0059403     2005-05-12
556 CAO0059403     1985-08-31
559 CAO0059680     2005-05-02
564 CAO0059680     2002-12-05
582 CAO0059680     2003-09-16
585 CAO0059680     1997-04-18
586 CAO0059680     2007-06-18
589 CAO0059680     2005-03-20
659 CAO0065288     2006-07-01
682 CAO0070694     2007-09-13
685 CAO0070694     2006-08-22
695 CAO0072477     2003-06-04
696 CAO0072477     2004-07-05
718 CAO0073505     1998-09-15
720 CAO0073505     1989-09-18
725 CAO0073505     2000-11-22
727 CAO0073505     1991-08-23

I have managed to order it by PROCHI number using orderBy(PROCHI, data=dd) 
what I need to do is order by PROCHI then by date of admission so that date
of admission is ascending by PROCHI ascending.  

 PROCHI            date_admission
2   CAO0000713      1999-10-25
4   CAO0000713      1999-12-11
21  CAO0001743     1987-09-17
25  CAO0001743     1989-05-04
26  CAO0001743     1989-05-17
27  CAO0001743     1996-09-12

I've also had a look at order using the following commands:

Just the straight dd[order(dd$PROCHI, dd$date_admission) ,]
and dd[ do.call(order, dd) ,]

but I'm not sure how I can achieve what I need.  

Thanks,
Natalie
-- 
View this message in context: http://n4.nabble.com/ordering-columns-in-a-data-frame-tp1587294p1587294.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list