3

My question is I have three columns of integers representing dates. If I use

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

for each individual column, it works. However, if I use sapply as

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

it does not work. Any ideas about how to solve the problem efficiently? The reproducible codes are as 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 does not change at all. but use

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

it can work.

jlhoward
  • 58,004
  • 7
  • 97
  • 140
MYjx
  • 4,157
  • 9
  • 38
  • 53

2 Answers2

2

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 you want the output as data frame you can use 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
DatamineR
  • 10,428
  • 3
  • 25
  • 45
1

You don't need to wrap the additional argument into a function. lapply(...) also accepts additional FUN arguments. Hence...

as.data.frame(lapply(data, as.Date, origin = "1970-01-01"))

...would work as well.

andschar
  • 3,504
  • 2
  • 27
  • 35