How to create variables in a dataframe as there are days between two dates?

Advertisements In a data.frame, I would like to create as many variables as there are days between two dates. Here’s my example below. Many thanks in advance ! mydf <- data.frame( ident = c("a","b","c"), start = c("2023-01-01","2023-01-06","2023-01-24"), end = c("2023-01-03","2023-01-12","2023-01-30") ) ident start end 1 a 2023-01-01 2023-01-03 2 b 2023-01-06 2023-01-12 3 c 2023-01-24… Read More How to create variables in a dataframe as there are days between two dates?

Lubridate not working when using floor_date

Advertisements Hi I have a piece of code that rounds a range of dates in dataframe the strange is that it works for a one dataframe but for the another dataframe it gives an error so for one dataframe I have this code > range(data1$seamless_user_reg_date) [1] "2021-05-03" "2022-07-11" > lubridate::floor_date(range(data1$seamless_user_reg_date), "month") [1] "2021-05-01" "2022-07-01" and… Read More Lubridate not working when using floor_date

Use %/% within a pipe

Advertisements I would like to get interval in days as an integer between two dates: library(tidyverse) # esp for lubridate date1 <- ymd(‘2022-01-01’) date2 <- ymd(‘2022-01-10’) day_interval <- interval(date1, date2) day_interval %/% days() # returns 9 But I want to combine the 2 last lines into one. Is this possible? Tried e.g: day_interval <- interval(date1,… Read More Use %/% within a pipe

Extract date which is in strange format (EURO MTH/MM/YYYY) in R

Advertisements I want to convert the date in the format MMYYYY from a column which is in this format EURO MTH/MM/YYYY in R. The Monat column is in character. structure(list(Monat = c("EURO MTH/07/2015", "EURO MTH/08/2015", "EURO MTH/09/2015", "EURO MTH/10/2015", "EURO MTH/11/2015", "EURO MTH/12/2015" ), Gesamtpreis = c(145667346.4375, 138659005.976562, 152031299.125, 127354805.953125, 141803880.734375, 146695055.171875)), row.names = c(NA,… Read More Extract date which is in strange format (EURO MTH/MM/YYYY) in R

R Error in `mutate()`: ! Problem while computing `Year = year(USER_Year)`. Caused by error in `as.POSIXlt.numeric()`: ! 'origin' must be supplied

Advertisements I know this question has been asked a lot of times but for some reason I still get this error even I though it should work based on the answers on this forum. Maybe I am missing a step. Basically, I would like to change USER_Year from num to date so the leaflet map… Read More R Error in `mutate()`: ! Problem while computing `Year = year(USER_Year)`. Caused by error in `as.POSIXlt.numeric()`: ! 'origin' must be supplied

Format date in R with lubridate

Advertisements My input data, formatted as character, looks like this "2020-07-10T00:00:00" I tried library(lubridate) mdy_hms("2020-07-10T00:00:00", format=’%Y-%m-%dT%H:%M:%S’, tz=Sys.timezone()) But I get [1] NA NA Warning message: All formats failed to parse. No formats found. I tried the more flexibel approach parse_date_time(), but without luck parse_date_time("2020-07-10T00:00:00", ‘%Y-%m-%dT%H:%M:%S’, tz=Sys.timezone()) How can I convert this date "2020-07-10T00:00:00" to a… Read More Format date in R with lubridate

data.table does not allow lubridate's fast_strptime

Advertisements Say I have this df df <- data.table(a = c(‘2022-01-20’, ‘2022-01-21’) ); df a 1: 2022-01-20 2: 2022-01-21 Note that lubridate is able to convert this character column to date properly fast_strptime(df$a, "%Y-%m-%d") [1] "2022-01-20 UTC" "2022-01-21 UTC" but when trying to store back to df data.table gives df[, a := fast_strptime(a, "%Y-%m-%d") ]… Read More data.table does not allow lubridate's fast_strptime

Converting H:M:S character variable to numeric/continuous scale (R)

Advertisements I am interested in visualizing twitter sentiment over a given topic per hour, and my variables are stored as follows: sapply(valence_hour,class) Time day mean_valence n "character" "numeric" "numeric" "integer" Here is a data example: Time day mean_valence n 23:59:00 19 0.0909090909 3 23:58:00 19 0.0589743590 3 23:57:00 19 0.49743590 3 I then ran the… Read More Converting H:M:S character variable to numeric/continuous scale (R)

Subsetting or filtering within dplyr::summarise

Advertisements There are multiple similar questions on this but not the same problem MWE: library(dplyr) library(lubridate) df= data.frame(id = c(1:5), type = c("a", "b", "b", "a", "b"), start = dmy(c("05/05/2005","06/06/2006", "07/07/2007", "08/08/2008", "09/09/2009")), finish = dmy(c("08/08/2008", "09/09/2009","02/02/2011","02/02/2011", NA)), not_used = c(F,T,F,T,F)) I want to produce a summary, grouped by type, including the total number of… Read More Subsetting or filtering within dplyr::summarise