开发者

format a Date column in a Data Frame

开发者 https://www.devze.com 2023-01-22 16:32 出处:网络
I\'ve the following data from a data frame Name,JoiningDate,AmtPaid Joe,12/31/09,1000 Amy,10/28/09,100 John,05/06/10,200

I've the following data from a data frame

Name,JoiningDate,AmtPaid
Joe,12/31/09,1000
Amy,10/28/09,100
John,05/06/10,200

The Joining Date is coming in as a fac开发者_如何学Gotor (when I sapply). How can I convert this to date and then sort on JoiningDate?


This should do it (where df is your dataframe)

df$JoiningDate <- as.Date(df$JoiningDate , format = "%m/%d/%y")

df[order(df$JoiningDate ),]


try this package, works wonders, and was made for date/time...

library(lubridate)
Portfolio$Date2 <- mdy(Portfolio.all$Date2)


The data.table package has its IDate class and functionalities similar to lubridate or the zoo package. You could do:

dt = data.table(
  Name = c('Joe', 'Amy', 'John'),
  JoiningDate = c('12/31/09', '10/28/09', '05/06/10'),
  AmtPaid = c(1000, 100, 200)
)

require(data.table)
dt[ , JoiningDate := as.IDate(JoiningDate, '%m/%d/%y') ]
0

精彩评论

暂无评论...
验证码 换一张
取 消