r - Apply function doesn't result in Date format -


i facing problem when apply apply function on date column. date not being displayed correctly. need on this

> head(dataf) [1] "4-sep-06"  "27-oct-06" "8-jan-07"  "28-jan-07" "5-jan-07"  "28-jan-07" > res <- apply(dataf, 2, dmy) > head(res)           datem [1,]    1157328000 [2,]    1161907200 [3,]    1168214400 [4,]    1169942400 [5,]    1167955200 [6,]    1169942400 

regardless of want output, should not need use apply or cousins when function dmy handles vectors. use

res <- dmy(dataf) 

having said that, if want learn how use apply, ananda's comment correct. following should give correct results.

res <- lapply(dataf, dmy) 

here more elaborate example replace dates in couple of columns:

dataf <- data.frame(x = c("4-sep-06", "27-oct-06", "8-jan-07",                            "28-jan-07", "5-jan-07", "28-jan-07"),                     y = c("4-jan-06", "27-jan-06", "8-feb-07",                            "28-feb-07", "5-mar-07", "28-mar-07"),                     z = c(1:6)) dataf #           x         y z # 1  4-sep-06  4-jan-06 1 # 2 27-oct-06 27-jan-06 2 # 3  8-jan-07  8-feb-07 3 # 4 28-jan-07 28-feb-07 4 # 5  5-jan-07  5-mar-07 5 # 6 28-jan-07 28-mar-07 6  library(lubridate) ## third column not date ## replace first 2 columns directly dataf[1:2] <- lapply(dataf[1:2], dmy) dataf #            x          y z # 1 2006-09-04 2006-01-04 1 # 2 2006-10-27 2006-01-27 2 # 3 2007-01-08 2007-02-08 3 # 4 2007-01-28 2007-02-28 4 # 5 2007-01-05 2007-03-05 5 # 6 2007-01-28 2007-03-28 6 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -