Saturday 15 September 2012

r - sapply as.Date function to change the time variable from integer to date does not work -



r - sapply as.Date function to change the time variable from integer to date does not work -

my question have 3 columns of integers representing dates. if utilize

as.date(x,origin="1970-01-01")

for each individual column, works. however, if utilize sapply

sapply(data,function(x)as.date(x,origin="1970-01-01"))

it not work. ideas how solve problem efficiently? reproducible codes below

data=data.frame(time1=c(10189,11655,10914,12013,10934),time2=c(11758,10696,9784,10725,11225)) sapply(data,function(x)as.date(x,origin="1970-01-01"))

the result not alter @ all. utilize

as.date(data$time1,origin="1970-01-01")

it can work.

use lapply:

> lapply(data,function(x) as.date(x,origin="1970-01-01")) $time1 [1] "1997-11-24" "2001-11-29" "1999-11-19" "2002-11-22" "1999-12-09" $time2 [1] "2002-03-12" "1999-04-15" "1996-10-15" "1999-05-14" "2000-09-25"

and if want output info frame can utilize as.data.frame():

> as.data.frame(lapply(data,function(x) as.date(x,origin="1970-01-01"))) time1 time2 1 1997-11-24 2002-03-12 2 2001-11-29 1999-04-15 3 1999-11-19 1996-10-15 4 2002-11-22 1999-05-14 5 1999-12-09 2000-09-25

r date sapply as.date

No comments:

Post a Comment