lubridate

dplyr filter not working with lubridate

筅森魡賤 提交于 2019-12-02 09:19:20
问题 I am having troubles understanding why the code below behaves how it behaves. Basically, the filter seems to be applied but then fails to work in subsequent calls. > library(dplyr) > library(lubridate) > > md1 <- data.frame(no = 1:4, time = c("12:30:00", "13:30:00", "14:30:00", "15:30:00")) > md1$time <- hms(md1$time) > md1 no time 1 1 12H 30M 0S 2 2 13H 30M 0S 3 3 14H 30M 0S 4 4 15H 30M 0S > md2 <- filter(md1, hour(time)<14) > md2 no time 1 1 12H 30M 0S 2 2 13H 30M 0S > hour(md2$time) [1] 12

Recognize time in R

拟墨画扇 提交于 2019-12-02 06:00:51
I am working on a dataset in R with a time variable like this: Time = data.frame("X1"=c(930,1130,914,1615)) The first one/two digits of X1 refers to hour and the last two refers to minute. I want to make R recognize it as a time variable. I try to use lubridate hm function but it didnt work probably because a ":" is missing between the hour and minute in my data. I also thought about using str_sub function to separate the hour and minute first and then put them together with a ":" in between and finally use the lubridate function but I dont know how to extract the hour since sometimes it is

dplyr filter not working with lubridate

旧巷老猫 提交于 2019-12-02 04:29:13
I am having troubles understanding why the code below behaves how it behaves. Basically, the filter seems to be applied but then fails to work in subsequent calls. > library(dplyr) > library(lubridate) > > md1 <- data.frame(no = 1:4, time = c("12:30:00", "13:30:00", "14:30:00", "15:30:00")) > md1$time <- hms(md1$time) > md1 no time 1 1 12H 30M 0S 2 2 13H 30M 0S 3 3 14H 30M 0S 4 4 15H 30M 0S > md2 <- filter(md1, hour(time)<14) > md2 no time 1 1 12H 30M 0S 2 2 13H 30M 0S > hour(md2$time) [1] 12 13 14 15 while I would like / expect to receive [1] 12 13 in the last call Any suggestions welcome. It

Converting yearmon column to last date of the month in R

巧了我就是萌 提交于 2019-12-02 01:09:25
I have a data frame (df) like the following: Date Arrivals 2014-07 100 2014-08 150 2014-09 200 I know that I can convert the yearmon dates to the first date of each month as follows: df$Date <- as.POSIXct(paste0(as.character(df[,1]),"-01"), format = "%Y-%m-%d") However, given that my data is not available until the end of the month I want to index it to the end rather than the beginning, and I cannot figure it out. Any help appreciated. If the Date variable is an actual yearmon class vector, from the zoo package, the as.Date.yearmon method can do what you want via its argument frac . Using

Subset dataframe based on POSIXct date and time greater than datetime using dplyr

旧时模样 提交于 2019-12-02 00:10:34
问题 I am not sure what is going wrong with selecting date times as a POSIXct format. I have read several comments on subsetting a dataframe based on as.Date and I can get that to work without an issue. I have also read many posts suggesting that filtering POSIXct formats should work, but for some reason I cannot get it to work. An example dataframe: library(lubridate) library(dplyr) date_test <- seq(ymd_hms('2016-07-01 00:00:00'),ymd_hms('2016-08-01 00:00:00'), by = '15 min') date_test <- data

Calculate character string “days, hours, minutes, seconds” to numeric total days [duplicate]

我们两清 提交于 2019-12-01 20:55:44
This question already has an answer here: R extract time components from semi-standard strings 2 answers I have seen a lot of questions relating to formatting times, but none in the particular imported format that I have: Time <- c( "22 hours 3 minutes 22 seconds", "170 hours 15 minutes 20 seconds", "39 seconds", "2 days 6 hours 44 minutes 17 seconds", "9 hours 54 minutes 36 seconds", "357 hours 23 minutes 28 seconds", "464 hours 30 minutes 7 seconds", "51 seconds", "31 hours 39 minutes 2 seconds", "355 hours 29 minutes 10 seconds") Some times contain only "seconds", and others "minutes and

Convert multiple columns to dates with lubridate and dplyr

試著忘記壹切 提交于 2019-12-01 20:18:35
I'm looking for a straightforward way to convert all of the variables in a data frame which begin with 'date' to dates using lubridate::dmy() (they are currently characters with the dmy format). I had thought there would be done with mutate_if or mutate_each in dplyr but I am struggling to figure out how. You can use mutate_at() library(dplyr) library(lubridate) df <- mutate_at(df, vars(starts_with("date")), funs(dmy)) or use mutate_if to mutate all date columns to dmy. Using is.Date from lubridate . df <- mutate_if(df, is.Date, funs(dmy)) 来源: https://stackoverflow.com/questions/43264068

Using ifelse with transform in ddply

北城余情 提交于 2019-12-01 17:55:16
I am trying to use ddply with transform to populate a new variable ( summary_Date ) in a dataframe with variables ID and Date . The value of the variable is chosen based on the length of the piece that is being evaluated using ifelse : If there are less than five observations for an ID in a given month, I want to have summary_Date be calculated by rounding the date to the nearest month (using round_date from package lubridate ); if there are more than five observations for an ID in a given month, I want the summary_Date to simply be Date . require(plyr) require(lubridate) test.df <- structure(

Parse dates in format dmy together with dmY using parse_date_time

老子叫甜甜 提交于 2019-12-01 16:45:33
I have a vector of character representation of dates, where formats mostly are dmY (e.g. 27-09-2013), dmy (e.g. 27-09-13), and occasionally some b or B months. Thus, parse_date_time in package lubridate that "allows the user to specify several format-orders to handle heterogeneous date-time character representations" could be a very useful function for me. However, it seems that parse_date_time has problem parsing dmy dates when they occur together with dmY dates. When parsing dmy alone, or dmy together with some other formats relevant to me, it works fine. This pattern was also noted in a

Find day of year with the lubridate package in R

偶尔善良 提交于 2019-12-01 15:08:08
I'm looking to find the day of year for a POSIXct class object with lubridate. For example, 12-9-2015 is day 343 . It's easy to find the day of the week or month with lubridate: > lubridate::wday("2015-12-09 04:27:56 EST", labels = T) Wed > lubridate::day("2015-12-09 04:27:56 EST") 9 Is there an easy way to do so for the day of the year? I've searched the documentation and other questions but have not (yet) found an answer. The correct function is yday , as in lubridate::yday(Sys.time()) 来源: https://stackoverflow.com/questions/34189367/find-day-of-year-with-the-lubridate-package-in-r