We do not need applyto MARGIN=1. Instead, we can pastecolumn on with(birds, paste(year, month, day, sep="-"))and wrap it as.Datefor conversion to the Date class. The result ymdis a POSIXctclass, internally apply, it will be forcibly numbered.
library(lubridate)
library(dplyr)
mutate(birds, date=ymd(paste(year, month, day)))
unite tidyr, POSIXct class
library(tidyr)
unite(birds, date, year:day, sep="-") %>%
mutate(date=ymd(date))
do.call base R ymd
birds$date <- ymd(do.call(paste, birds))
as.Date base R
as.Date(do.call(paste, c(birds,sep="-")))
, apply
res <- apply(birds[,c("year","month","day")], 1,
function(x) ymd(paste(x[1], x[2], x[3], sep="-")))
unname(as.POSIXct(res, origin='1970-01-01',tz='UTC'))
#[1] "2015-05-13 UTC" "2015-05-14 UTC" "2015-05-15 UTC" "2015-05-16 UTC"
#[5] "2015-05-17 UTC" "2014-05-28 UTC" "2014-05-29 UTC" "2014-05-30 UTC"
#[9] "2014-05-31 UTC" "2014-06-01 UTC" "2013-05-08 UTC" "2013-05-09 UTC"
#[13] "2013-05-10 UTC" "2013-05-11 UTC" "2013-05-12 UTC"
birds <- structure(list(year = c(2015L, 2015L, 2015L, 2015L, 2015L,
2014L,
2014L, 2014L, 2014L, 2014L, 2013L, 2013L, 2013L, 2013L, 2013L
), month = c(5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 5L, 5L,
5L, 5L, 5L), day = c(13L, 14L, 15L, 16L, 17L, 28L, 29L, 30L,
31L, 1L, 8L, 9L, 10L, 11L, 12L)), .Names = c("year", "month",
"day"), class = "data.frame", row.names = c("1", "2", "3", "4",
"5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15"))