Dear colleagues,
Is there a way to order list items by date? I have a series of surveys in a list where the name of each list item is the date the survey was taken but the list items are out of order. Each data frame has a variable in it with the survey date as well, if that helps.
Yours, Simon Kiss
#Sample Data
mylist<-list('1991-01-01'=data.frame(a=rep(5,5), survey.date=rep(as.Date('1991-01-01', format='%Y-%m-%d'))), '1979-01-01'=data.frame(aa=rep(5,5), survey.date=rep(as.Date('1979-01-01', format='%Y-%m-%d'), 5)), '2001-01-01'=data.frame(c=rep(6,5), survey.date=rep(as.Date('2001-01-01', format='%Y-%m-%d'), 5)))
mylist
Ordering List Items Chronologically
3 messages · Simon Kiss, William Dunlap, Sarah Goslee
o <- order(as.Date(names(mylist))) myListInDateOrder <- mylist[o] Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
Of Simon Kiss
Sent: Tuesday, November 20, 2012 8:41 AM
To: r-help at r-project.org
Subject: [R] Ordering List Items Chronologically
Dear colleagues,
Is there a way to order list items by date? I have a series of surveys in a list where the
name of each list item is the date the survey was taken but the list items are out of order.
Each data frame has a variable in it with the survey date as well, if that helps.
Yours, Simon Kiss
#Sample Data
mylist<-list('1991-01-01'=data.frame(a=rep(5,5), survey.date=rep(as.Date('1991-01-01',
format='%Y-%m-%d'))), '1979-01-01'=data.frame(aa=rep(5,5),
survey.date=rep(as.Date('1979-01-01', format='%Y-%m-%d'), 5)), '2001-01-
01'=data.frame(c=rep(6,5), survey.date=rep(as.Date('2001-01-01', format='%Y-%m-%d'),
5)))
mylist
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
As long as all your names of list elements are in the same format, this should do it: mylist[order(names(mylist))] Thanks for the reproducible example, Sarah
On Tue, Nov 20, 2012 at 11:40 AM, Simon Kiss <sjkiss at gmail.com> wrote:
Dear colleagues,
Is there a way to order list items by date? I have a series of surveys in a list where the name of each list item is the date the survey was taken but the list items are out of order. Each data frame has a variable in it with the survey date as well, if that helps.
Yours, Simon Kiss
#Sample Data
mylist<-list('1991-01-01'=data.frame(a=rep(5,5), survey.date=rep(as.Date('1991-01-01', format='%Y-%m-%d'))), '1979-01-01'=data.frame(aa=rep(5,5), survey.date=rep(as.Date('1979-01-01', format='%Y-%m-%d'), 5)), '2001-01-01'=data.frame(c=rep(6,5), survey.date=rep(as.Date('2001-01-01', format='%Y-%m-%d'), 5)))
mylist
-- Sarah Goslee http://www.functionaldiversity.org