posixct

Using sapply on vector of POSIXct

青春壹個敷衍的年華 提交于 2019-12-05 04:25:33
I have what may be a very simple question. I want to process a column of POSIXct objects from a dataframe and generate a vector of datetime strings. I tried to use the following sapply call dt <- sapply(df$datetime, function(x) format(x,"%Y-%m-%dT%H:%M:%S")) but to no avail. I keep getting the following error: > Error in prettyNum(.Internal(format(x, trim, digits, nsmall, width, 3L, : invalid 'trim' argument When I apply this function to a single POSIXct object from the column, I have no problem. So I'm stumped at the moment about what the problem is. Do I need to do something special with

Determine season from Date using lubridate in R

独自空忆成欢 提交于 2019-12-05 01:58:24
问题 I have a very big dataset with a DateTime Column containing POSIXct-Values. I need to determine the season (Winter - Summer) based on the DateTime column. I've created a function which works fine on a small dataset, but crashes when I use it on the large one. Can anybody see my mistake? I've created 4 functions: 3 subfunctions so that I can do logical comparisons and selection using *apply functions 1 function to determine the season Here are thefunctions: require(lubridate) # function for

05:00:00 - 28:59:59 time format

独自空忆成欢 提交于 2019-12-05 01:28:08
I have dataset where time.start vary from 5:00:00 to 28:59:59 (i.e. 01.01.2013 28:00:00 is actually 02.01.2013 04:00:00). Dates are in %d.%m.%Y format. Date Time.start 01.01.2013 22:13:07 01.01.2013 22:52:23 01.01.2013 23:34:06 01.01.2013 23:44:25 01.01.2013 27:18:48 01.01.2013 28:41:04 I want to convert it to normal date format. dates$date <- paste(dates$Date,dates$Time.start, sep = " ") dates$date <- as.POSIXct(strptime(dates$date, "%m.%d.%Y %H:%M:%S")) But obviously I have NA for time > 23:59:59 How should I modify my code? E.g. add the time as seconds to the date: df <- read.table(header=T

Is there a specific way to handle timestamp columns in R when pulling data using RPostgreSQL?

核能气质少年 提交于 2019-12-04 21:20:15
问题 I'm trying to pull data from a PostgreSQL database and the results for a timestamp field are inconsistent. I'm not sure if I'm handling POSIXct results properly. Otherwise, I think I found a bug in the RPostgreSQL package. Here is the way to replicate the issue: Suppose there is a table in a postgres database with one field (run this in PostgreSQL): CREATE DATABASE mydb; CREATE TABLE test_table ( "DateTime" timestamp without time zone NOT NULL, CONSTRAINT "pk_test_table" PRIMARY KEY (

How to expand Posixct field in R str()?

淺唱寂寞╮ 提交于 2019-12-04 17:40:36
问题 I am trying to expand the amount of factors shown in one custom Posixct field where the normal way ( str(DF, list.len=ncol(DF), vec.len=20) ) does not work. I request here 20 but it shows all the time two ( "2017-01-01 08:40:00" "2017-01-01 08:50:00" ... ) regardless the length of the list (here 3 ). Data data.csv "AAA", "BBB" 1, 01012017-0940+0100 2, 01012017-0950+0100 3, 01012017-0838+0100 Code library('methods') # setClass # https://unix.stackexchange.com/a/363290/16920 setClass('iso8601')

R as.POSIXct() dropping hours minutes and seconds

懵懂的女人 提交于 2019-12-04 09:17:31
I am experimenting with R to analyse some measurement data. I have a .csv file containing more than 2 million lines of measurement. Here is an example: 2014-10-22 21:07:03+00:00,7432442.0 2014-10-22 21:07:21+00:00,7432443.0 2014-10-22 21:07:39+00:00,7432444.0 2014-10-22 21:07:57+00:00,7432445.0 2014-10-22 21:08:15+00:00,7432446.0 2014-10-22 21:08:33+00:00,7432447.0 2014-10-22 21:08:52+00:00,7432448.0 2014-10-22 21:09:10+00:00,7432449.0 2014-10-22 21:09:28+00:00,7432450.0 After reading in the file, I want to convert the time to a correct time, using as.POSIXct() . For small files this works

time zones in POSIXct and xts, converting from GMT in R

一笑奈何 提交于 2019-12-04 06:30:57
I have a bunch of 1 minute returns in an xts object with the index being POSIXct and time zone being GMT. The returns are on NYSE so I would like to convert to the eastern time zone but I would like to take care of the daylight savings time properly. What is the best way of doing this? I am a bit confused between the EST timezone and the EDT timezone. I would like my times to convert properly to the NY time in winter and summer. Use indexTZ<- and the America/New_York timezone > tail(SPY) SPY.Bid.Price SPY.Ask.Price SPY.Trade.Price SPY.Mid.Price SPY.Volume 2012-08-09 19:54:00 140.47 140.48 140

Improve performance of data.table date+time pasting?

纵然是瞬间 提交于 2019-12-04 05:26:40
I am not sure that I can ask this question here, let me know if I should do it somewhere else. I have a data.table with 1e6 rows having this structure: V1 V2 V3 1: 03/09/2011 08:05:40 1145.0 2: 03/09/2011 08:06:01 1207.3 3: 03/09/2011 08:06:17 1198.8 4: 03/09/2011 08:06:20 1158.4 5: 03/09/2011 08:06:40 1112.2 6: 03/09/2011 08:06:59 1199.3 I am converting the V1 and V2 variables to a unique datetime variable, using this code: system.time(DT[,`:=`(index= as.POSIXct(paste(V1,V2), format='%d/%m/%Y %H:%M:%S'), V1=NULL,V2=NULL)]) user system elapsed 47.47 0.16 50.27 Is there any method to improve

How to make an empty vector of POSIXct

孤街醉人 提交于 2019-12-03 22:25:09
I want to make an empty vector of POSIXct so that I can put a POSIXct in it: vec <- vector("POSIXct", 10) vec vec[1] <- "2014-10-27 18:11:36 PDT" vec That does not work. Any ideas? Since there is no POSIX mode , you cannot initialize a POSIXct vector with vector() alone (see ?mode for a list of all mode types). But we can use .POSIXct to create the vector from a character vector. (x <- .POSIXct(character(10))) ## the same as .POSIXct(vector("character", 10)) # [1] NA NA NA NA NA NA NA NA NA NA class(x) # [1] "POSIXct" "POSIXt" Also note that you can also use .POSIXct(integer(10)) for a length

Using the result of summarise (dplyr) to mutate the original dataframe

隐身守侯 提交于 2019-12-03 18:05:13
问题 I have a rather big dataframe with a column of POSIXct datetimes (~10yr of hourly data). I would flag all the rows in which the day falls in a Daylight saving period. For example if the Daylight shift starts on '2000-04-02 03:00:00' (DOY=93) i would like that the two previous hours of DOY=93 could be flagged. Although I am a newbie of dplyr I would use this package as much as possible and avoid for-loops as much as possible For example: library(lubridate) sd = ymd('2000-01-01',tz="America