5

I'm an experienced Pandas user and am having trouble plugging values from my R frame into a function.

The following function works with hard coded values

>seq.Date(as.Date('2018-01-01'), as.Date('2018-01-31'), 'days') 

 [1] "2018-01-01" "2018-01-02" "2018-01-03" "2018-01-04" "2018-01-05" "2018-01-06" "2018-01-07"
 [8] "2018-01-08" "2018-01-09" "2018-01-10" "2018-01-11" "2018-01-12" "2018-01-13" "2018-01-14"
[15] "2018-01-15" "2018-01-16" "2018-01-17" "2018-01-18" "2018-01-19" "2018-01-20" "2018-01-21"
[22] "2018-01-22" "2018-01-23" "2018-01-24" "2018-01-25" "2018-01-26" "2018-01-27" "2018-01-28"
[29] "2018-01-29" "2018-01-30" "2018-01-31"

Here is an extract from a dataframe I'm using

>df[1,1:2]
# A tibble: 1 x 2
  start_time end_time  
  <date>     <date>    
1 2017-04-27 2017-05-11

When plugging these values into the 'seq.Date' function I get an error

> seq.Date(from=df[1,1], to=df[1,2], 'days')
Error in seq.Date(from = df[1, 1], to = df[1, 2], "days") : 
'from' must be a "Date" object

I suspect this is because subsetting using df[x,y] returns a tibble rather than the specific value

data.class(df[1,1])
[1] "tbl_df"

What I'm hoping to derive is a sequence of dates. I need to be able to point this at various places around the dataframe.

Many thanks for any help!

Clem Manger
  • 173
  • 1
  • 12
  • There's often no need to call the S3 method `seq.Date` directly, just call `seq()` and R will dispatch to the correct method for you. – SymbolixAU Apr 24 '18 at 08:31
  • 1
    Also, the `tibble` implementation of `[` changes the behaviour compared to regular `data.frames`. It [defaults to `drop = FALSE`](https://github.com/tidyverse/tibble/blob/master/R/tbl-df.r#L57), so if you're used to working with base R you'll get tripped up, which defaults to `drop = TRUE`. – SymbolixAU Apr 24 '18 at 08:36

3 Answers3

4

Just use double brackets:

seq.Date(from=df[[1,1]], to=df[[1,2]], 'days')
r.user.05apr
  • 5,356
  • 3
  • 22
  • 39
2

The extraction functions of tibble may not return vectors but one column tibbles, use dplyr::pull to extract the column as vector, like in this answer: Extract a dplyr tbl column as a vector

snaut
  • 2,261
  • 18
  • 37
2

Another option is to set the drop argument in the `[` function to TRUE.

If TRUE the result is coerced to the lowest possible dimension

seq.Date(from = df[1, 1, drop = TRUE], to = df[1, 2, drop = TRUE], 'days')
# [1] "2017-04-27" "2017-04-28" "2017-04-29" "2017-04-30" "2017-05-01" "2017-05-02" "2017-05-03" "2017-05-04" "2017-05-05" "2017-05-06"
#[11] "2017-05-07" "2017-05-08" "2017-05-09" "2017-05-10" "2017-05-11"

data

df <- tibble(start_time = as.Date('2017-04-27'), 
             end_time = as.Date('2017-05-11'))
markus
  • 25,843
  • 5
  • 39
  • 58