How to obtain a date value from a tibble?

How to obtain a date value from a tibble?

...do not know how to convert...

I can only get it, converting the whole tibble to a data.frame.

library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union
the_date <- "2022-11-26"
(ymd(the_date)) |> str()
#>  Date[1:1], format: "2022-11-26"

Created on 2022-11-26 by the reprex package (v2.0.1)

Adjust ymd to reflect your date format. See the help() page.

Thank you, that was not the problem.
The problem is to access it in a tibble instead of a data frame.

Solution:
refDate <- tbl[['steal_my_time']][1] # tibble-date-hint

The way to get around this is to use $ or [[ , as you would when subsetting a list:

Tibbles are a superset of data frames. Anything that works on a data frame will work on a tibble.

Except dates, believe me. I spent hours with it, on different occasions...
Try this with a data frame and then with a tibble.

 lastDate    <- as.character(df[1,'lastDate'])
 lastDate   <- lubridate::ymd(lastDate)

You're right and I'm wrong:

Tibbles are quite strict about subsetting. [ always returns another tibble. Contrast this with a data frame: sometimes [ returns a data frame and sometimes it just returns a vector. Vignette.

That accounts for this:

dat <- mtcars[1:5]
dat$lastDate <- seq(from = as.Date("2022-11-26"),by = "day",length.out = dim(dat)[1])
str(dat)
#> 'data.frame':    32 obs. of  6 variables:
#>  $ mpg     : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
#>  $ cyl     : num  6 6 4 6 8 6 8 4 4 6 ...
#>  $ disp    : num  160 160 108 258 360 ...
#>  $ hp      : num  110 110 93 110 175 105 245 62 95 123 ...
#>  $ drat    : num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
#>  $ lastDate: Date, format: "2022-11-26" "2022-11-27" ...
tib <- dplyr::as_tibble(dat)
str(tib)
#> tibble [32 × 6] (S3: tbl_df/tbl/data.frame)
#>  $ mpg     : num [1:32] 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
#>  $ cyl     : num [1:32] 6 6 4 6 8 6 8 4 4 6 ...
#>  $ disp    : num [1:32] 160 160 108 258 360 ...
#>  $ hp      : num [1:32] 110 110 93 110 175 105 245 62 95 123 ...
#>  $ drat    : num [1:32] 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
#>  $ lastDate: Date[1:32], format: "2022-11-26" "2022-11-27" ...

(from_dat <- as.character(dat[1,'lastDate']))
#> [1] "2022-11-26"
(from_tib <- as.character(tib[1,'lastDate']))
#> [1] "19322"

(from_dat <- as.character(dat[1,'lastDate'][[1]]))
#> [1] "2022-11-26"
(from_tib <- as.character(tib[1,'lastDate'][[1]]))
#> [1] "2022-11-26"

which, in turn, means that tibble forces an additional step

dat <- mtcars[1:5]
dat$lastDate <- seq(from = as.Date("2022-11-26"),by = "day",length.out = dim(dat)[1])
tib <- dplyr::as_tibble(dat)
from_tib <- as.character(tib[1,'lastDate'])
lubridate::as_date(as.numeric(from_tib))
#> [1] "2022-11-26"

Always just powered through with the [[1]] stuff in dealing with tibbles. Now, I knowwhy and now I'm even more pleased to have gone over to {data.table}

Nice one!
I tried and hated tibbles for years.
Now, I m an old man and what should I say... I use them more more. It would take too long, to change again and go with data tables :-), and most things I use 'talk tibbles'.

1 Like

I didn't start using data table until my mid-70s, so I guess my days are numbered :grin:

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.