Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access R Dataframe Values Rather than Tibble

Tags:

date

r

seq

tibble

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!

like image 516
Clem Manger Avatar asked Apr 24 '18 08:04

Clem Manger


People also ask

What is the difference between Tibble and Dataframe in R?

There are three key differences between tibbles and data frames: printing, subsetting, and recycling rules.

Is Tibble the same as Dataframe?

Tibble is a modernized version of a data frame used in the tidyverse. They use several techniques to make them 'smarter' - for example lazy loading.

Can you convert Tibble to Dataframe?

By default, the read_csv() function imports the CSV file as a tibble. What is this? We can see that the tibble has been successfully converted to a data frame.

Which is the function used to convert a data frame to a Tibble?

Use as_tibble() to turn an existing object into a tibble. Use enframe() to convert a named vector into a tibble.


2 Answers

Just use double brackets:

seq.Date(from=df[[1,1]], to=df[[1,2]], 'days')
like image 84
r.user.05apr Avatar answered Sep 23 '22 00:09

r.user.05apr


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

like image 22
snaut Avatar answered Sep 26 '22 00:09

snaut