posixct

R - How can I add an empty POSIXct column to a data.frame / tibble which already exists?

拟墨画扇 提交于 2019-12-02 03:10:40
问题 I can initialize a data frame with a POSIXct column with code like this: df <- data.frame(a=numeric(), b=character(), c=as.POSIXct(character())) However, if I try to add an empty POSIXct column to a data.frame or tibble which already exists, the column is transformed to numeric type/class. > df <- tibble("Index"=numeric(10)) > df[,"date"] <- as.POSIXct(character()) > df[,"date"] %>% pull %>% class() [1] "numeric Is there a method to overcome this problem? 回答1: would this work for you (most

Determine if 24 hour datetime is within interval

☆樱花仙子☆ 提交于 2019-12-02 02:57:57
问题 Hope you can help. Have a dataframe with date times in it. I'd like to determine if the time result occurs after hours (> 16:00). Is there an easy way to do this? Was planning on converting the time to seconds and then doing like this but suppose there's an easier way to do this via R. datetimes <- c("2013-04-01 08:19:00", "2013-04-02 16:19:00", "2017-02-17 14:01:00", "2017-02-17 22:01:00") as.POSIXct(datetimes) 回答1: Just use a format and comparsion via >= : format(datetimes,"%H") >= 16 #[1]

Conversion from character to date/time returns NA

大憨熊 提交于 2019-12-02 01:51:09
问题 I often use as.POSIXct to convert characters to POSIXct , but I get NA sometimes and I don't know why. For example: DATE <- "Fri Apr 10 11:57:47 2015" DATE_in_posix <- as.POSIXct(DATE, format="%a %b %d %H:%M:%S %Y") I tried this too: DATE_in_posix <- as.POSIXct(DATE, format="%a %h %d %H:%M:%S %Y") But result for both is always: > DATE_in_posix [1] NA Maybe the input for as.POSIXct is too long? And when it's too long what could be the solution? 回答1: It's probably because "Fri" and "Apr" are

How to combine R dataframes based constraints on a time column

不问归期 提交于 2019-12-02 01:46:34
I have two R tables, each with a list of users and a timestamp corresponding to the time that they took a certain action. The first of these ( df1 ) two tables has an exhaustive list of the users, and users will have multiple rows with different timestamps. The second ( df2 ) will have a more limited list of users, but again users will be in the table multiple times with different timestamps. What I'd like to be able to do is join the two tables and end up with a table that matched the user in df1 with the closest timestamp in df2 , as long as the timestamp in df2 happened after the one in df1

R - How can I add an empty POSIXct column to a data.frame / tibble which already exists?

久未见 提交于 2019-12-02 00:55:21
I can initialize a data frame with a POSIXct column with code like this: df <- data.frame(a=numeric(), b=character(), c=as.POSIXct(character())) However, if I try to add an empty POSIXct column to a data.frame or tibble which already exists, the column is transformed to numeric type/class. > df <- tibble("Index"=numeric(10)) > df[,"date"] <- as.POSIXct(character()) > df[,"date"] %>% pull %>% class() [1] "numeric Is there a method to overcome this problem? would this work for you (most doing what eipi10 suggest in his comment ) library(tibble) # install.packages(c("dplyr"), dependencies = TRUE)

Wrong units displayed in data.table with POSIXct arithmetic

不问归期 提交于 2019-12-02 00:43:49
问题 When durations are computed in data.table (v1.9.2), the wrong units can be printed with POSIXct arithmetic. It seems the first units are chosen. require("data.table") dt <- data.table(id=c(1,1,2,2), event=rep(c("start", "end"), times=2), time=c(as.POSIXct(c("2014-01-31 06:05:30", "2014-01-31 06:45:30", "2014-01-31 08:10:00", "2014-01-31 09:30:00")))) dt$time[2] - dt$time[1] # in minutes dt$time[4] - dt$time[3] # in hours dt[ , max(time) - min(time), by=id] # wrong units printed for id 2 I

Determine if 24 hour datetime is within interval

烂漫一生 提交于 2019-12-02 00:37:17
Hope you can help. Have a dataframe with date times in it. I'd like to determine if the time result occurs after hours (> 16:00). Is there an easy way to do this? Was planning on converting the time to seconds and then doing like this but suppose there's an easier way to do this via R. datetimes <- c("2013-04-01 08:19:00", "2013-04-02 16:19:00", "2017-02-17 14:01:00", "2017-02-17 22:01:00") as.POSIXct(datetimes) Just use a format and comparsion via >= : format(datetimes,"%H") >= 16 #[1] FALSE TRUE FALSE TRUE Assuming you have already converted via as.POSIXct() as shown in your question. Even

How to calculate number of days between two dates in R

我是研究僧i 提交于 2019-12-02 00:11:02
问题 I'm trying to subtract two dates in R. These are the two dates via the structure command: str(standard_data_4testing$start_datetime) POSIXct[1:489124], format: "2016-02-01 00:38:49" "2016-02-01 07:48:53" "2016-02-01 08:32:08" "2016-02-01 11:21:13" ... str(standard_data_4testing$original_installdate) Date[1:489124], format: "2015-10-15" "2015-10-15" "2015-10-15" "2016-01-29" "2016-01-29" "2016-01-29" ... I created both with as.Date functions in R but start_datetime has date and time and

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

POSIXct date conversion error [duplicate]

不问归期 提交于 2019-12-01 23:22:41
This question already has an answer here: R: strptime() and is.na () unexpected results 1 answer I've encountered the following error when converting a set of dates in character format to a POSIXct object. Example Data: t<-c("3/11/2007 1:30", "3/11/2007 2:00", "4/11/2007 2:00") str(t) chr [1:3] "3/11/2007 1:30" "3/11/2007 2:00" "4/11/2007 2:00" z<-as.POSIXct(strptime(t, format ="%m/%d/%Y %H:%M")) z "2007-03-11 01:30:00 MST" NA "2007-04-11 02:00:00 MDT" str(z) POSIXct[1:3], format: "2007-03-11 01:30:00" NA "2007-04-11 02:00:00" My question is why is the NA returned for the second date in z? I