lubridate

R: Convert month and 2 digit year into date

你说的曾经没有我的故事 提交于 2019-12-24 07:35:25
问题 How can this date string be converted in R and lubridate ? lubridate::as_date('Apr-78', format = '%B-%Y') How to prevent the error invalid 'tz' ? 回答1: lubridate::parse_date_time("Apr-78", 'my') 回答2: Others have already mentioned that your format isn't quite correct, so watch out for that. As far as timezones are concerned: My first thought was that you simply need to add tz = "UTC" (or some other timezone), but the fact that your date has no day information is the bigger issue. If you don't

Time zone gets lost with lubridate when creating a new datetime component from it

我的未来我决定 提交于 2019-12-24 03:55:34
问题 The following data frame is from a dput . I have used forced_tz on the datatime with no argument. I live in UTC+1 time zone. library(lubridate) library(dplyr) df <- structure(list(value = structure(c(1514967058, 1515148132, 1517472989, 1543844646, 1525085884, 1520584330, 1522838681, 1540379051, 1516707360, 1516705706), class = c("POSIXct", "POSIXt"))), .Names = "value", row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame")) tz(df$value) [1] "" df2 <- df %>% mutate(year=year(value)

Parse datetime with lubridate

会有一股神秘感。 提交于 2019-12-23 22:44:01
问题 I am trying to parse the following datetime with the following format: library(lubridate) a <- "2004-05-07 18:24:58.666424" I tried the following, but returned NAs b <- lubridate::mdy_hms(a) c <- lubridate::mdy(a) Could anyone please explain how to parse this. I am also fine if lubridate is not used. 回答1: With lubridate , you can specify that your seconds have a decimal with the special S! or OS formats; see ?parse_date_time for more parsing options. > parse_date_time("2004-05-07 18:24:58

Tidyr's gather() with NAs

拥有回忆 提交于 2019-12-23 19:07:32
问题 I am using tidyr and lubridate to convert a wide table to a long table. The following works just fine. > (df <- data.frame(hh_id = 1:2, bday_01 = ymd(20150309), bday_02 = ymd(19850911), bday_03 = ymd(19801231))) hh_id bday_01 bday_02 bday_03 1 1 2015-03-09 1985-09-11 1980-12-31 2 2 2015-03-09 1985-09-11 1980-12-31 > gather(df, person_num, bday, starts_with("bday_0")) hh_id person_num bday 1 1 bday_01 2015-03-09 2 2 bday_01 2015-03-09 3 1 bday_02 1985-09-11 4 2 bday_02 1985-09-11 5 1 bday_03

R - Join Dataframes using a key and then Approximate Dates

邮差的信 提交于 2019-12-23 17:09:43
问题 The Problem I am trying to merge two dataframes using 3 ID columns (Or 1 column, if I paste the 3 together), one of which is a datetime variable and can vary between the two dataframes by up to 1 second. Background I have two dataframes extracted from a library with transaction records. For some reason, the check-outs and the check-ins are recorded seperately, without a unique "transaction ID" to match them. I'd like to match them. The "check-out" dataframe has a record for each item that was

R: how to resample intraday data at the group level?

喜夏-厌秋 提交于 2019-12-23 15:57:41
问题 Consider the following dataframe time <-c('2016-04-13 23:07:45','2016-04-13 23:07:50','2016-04-13 23:08:45','2016-04-13 23:08:45' ,'2016-04-13 23:08:45','2016-04-13 23:07:50','2016-04-13 23:07:51') group <-c('A','A','A','B','B','B','B') value<- c(5,10,2,2,NA,1,4) df<-data.frame(time,group,value) > df time group value 1 2016-04-13 23:07:45 A 5 2 2016-04-13 23:07:50 A 10 3 2016-04-13 23:08:45 A 2 4 2016-04-13 23:08:45 B 2 5 2016-04-13 23:08:45 B NA 6 2016-04-13 23:07:50 B 1 7 2016-04-13 23:07

R- date time variable loses format after ifelse

倾然丶 夕夏残阳落幕 提交于 2019-12-23 15:13:09
问题 I have a variable in the proper POSIXct format, converted with ymd_hms(DateTime) {lubridate}. However, after a transformation the variable loses its POSIXct format: daily$DateTime<- ifelse(daily$ID %in% "r1_1"|daily$ID %in% "r1_2", NA,daily$DateTime) I try to convert the variable again to POSIXct with lubridate, but it seems it does´t like the NAs, and, in addition, now the variable DateTime has a num format that lubridate does´t recognise as a date and time format (e.g. 1377419400). Please,

How to plot data averaged by month and day?

柔情痞子 提交于 2019-12-23 05:07:09
问题 I would like to reproduce that plot: from link I'm almost able to do that but I miss something: library(lubridate) library(ggplot2) # get the data dates <- seq(as.Date("2012-01-01"), as.Date("2014-12-30"), by = 1) series <- seq(from = 1, to = 1095, by = 1) df<-data.frame(dates,series) # for aggregation df$month<-as.numeric(format(df$date, "%m")) df$week<-week(as.POSIXct(df$date)) df$weekday<-weekdays(as.POSIXct(df$date)) df$days<-as.numeric(format(df$date, "%d")) df$week_days<-as.numeric

How to subset and extract time series by time interval in row

回眸只為那壹抹淺笑 提交于 2019-12-23 02:36:39
问题 I am working on an analysis of animal locations that requires locations for each animal to be 60 minutes or greater apart. Time differences in locations among animals does not matter. The data set has a list of animal IDs and date and time of each location, example below. For example, for animal 6 below, starting at the 16:19 location, the code would iterate through locations until it finds a location that is 60+ minutes from 16:19. In this case it would be the 17:36 location. Then, the code

Add business days without Saturday sunday and specific holidays in R

泄露秘密 提交于 2019-12-23 02:29:31
问题 I want to add days to a date in R, except Saturdays and Sundays and specific holidays. Suppose I have a dateset : d <- dmy("25-7-2016","26-7-2016") days <- c(3:4) data <- data.frame(d,days) data I want to add #days (days column) to a date (d column) I have tried the following code: library(bizdays) library(lubridate) cal <- Calendar(weekdays=c('sunday', 'saturday')) data$f <- offset(d, days, cal) data I can get the days without considering Saturdays and Sundays. But I want to exclude a