i have data frame df 1 of columns being date/time , order data frame in descending order of column.
df <- data.frame(id=c('id3', 'id2','id1'), end=c('4/1/10 12:00', '6/1/11 14:20', '1/1/09 11:10'), age=c(40,30,20));
i first converted end
column et
using et = as.posixct(df$end,format='%m/%d/%y %h:%m')
, , used following, got error unary operator '-' not accepted argument :
out <- df[order(-df$et),];
i tried used descending flag again got error arguments not being of same length.
out <- df[order(df$et, descending=true),];
however, ascending order seems work: out <- df[order(df$et),]
.
how can order in descending order (most recent time first)? thank you.
i think work:
## bigger dataset 2 times on same day: df <- data.frame(id=c('id3', 'id2','id1','id4'), end=c('4/1/10 12:00', '6/1/11 14:20', '1/1/09 11:10' , '1/1/09 13:11'), age=c(40,30,20,20)); ## note self - include timezone. df$dtime <- as.posixct( df$end , format = "%d/%m/%y %h:%m" , tz = "gmt") df[ order(df$dtime , decreasing = true ),] # id end age dtime #2 id2 6/1/11 14:20 30 2011-01-06 14:20:00 #1 id3 4/1/10 12:00 40 2010-01-04 12:00:00 #4 id4 1/1/09 13:11 20 2009-01-01 13:11:00 #3 id1 1/1/09 11:10 20 2009-01-01 11:10:00
Comments
Post a Comment