Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upwith_tz to support dataframes? #344
Comments
|
I think these sort of things must be part of generic data manipulation packages. What is the most concise pattern to do it right now?
@hadley any tricks with plyr/dplyr? This is a job for an idempotent conditional map like:
In any case, if we are taking this path we probably should enhance other functions as well. Could you please go through the list of functions in lubridate and iden If we are going on this path then we |
|
To answer a question that @hadley posed in another issue, maybe I can try to explain the problem I'm trying to solve. When I know the columns of a dataframe, I know this is in a grey area between dplyr and lubridate, so I flipped a coin :) My workaround is a utility function that borrows from dplyr and lubridate: df_set_tz <- function(df, tz = "UTC"){
fn_set_tz <- function(x){
if (!identical(dplyr::type_sum(x), "time"))
return(x) # do nothing
lubridate::with_tz(x, tz)
}
dplyr::as_data_frame(lapply(df, fn_set_tz))
} |
|
Hi Vitalie,
This is exactly what
I think you can replace the |
|
Thanks @lionel- . Didn't know about purrr. I was almost there to start writing a functional vector manipulation library myself. |
|
I think this is related so I will add to the discussion here. I have a dataframe with column of timestamps in UTC and another column with the local timezone. I need to create a new column with the timestamp converted to the local time. library(dplyr)
data <- data_frame(
timestamp_utc = as.POSIXct(c('2015-11-18 03:55:04', '2015-11-18 03:55:08',
'2015-11-18 03:55:10'), tz = "UTC"),
local_tz = c('America/New_York', 'America/Los_Angeles',
'America/Indiana/Indianapolis')
)This doesn't work in a chain of other data %>%
mutate(timestamp_local = with_tz(timestamp_utc, tzone = local_tz))This does but obviously is slow. (Answer from StackOverflow.) data %>%
rowwise() %>%
mutate(timestamp_local = with_tz(timestamp_utc, tzone = local_tz))I tend to use |
|
I've moved this over to a different issue #359. It turns out it isn't related to this issue the more I read. |
|
Cool! Thanks! |
Could we consider adapting
with_tz()(and possiblyforce_tz()) to accept dataframes as well as vectors?This is a follow-on to tidyverse/readr#230.
The idea would be that if the first arg is a vector,
with_tz()would behave as it does currently.If the first arg is a dataframe,
with_tz()would loop over all the time-columns of the dataframe, and callwith_tz()on them.If you like, I could work up a PR.
Thanks,
Ian