posixct

Using sapply on vector of POSIXct

这一生的挚爱 提交于 2020-01-02 02:35:10
问题 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

How do I convert correctly timezones

时光总嘲笑我的痴心妄想 提交于 2020-01-01 03:42:06
问题 I am using the fasttime package for its fastPOSIXct function that can read character datetimes very efficiently. My problem is that it can only read character datetimes THAT ARE EXPRESSED IN GMT. R) fastPOSIXct("2010-03-15 12:37:17.223",tz="GMT") #very fast [1] "2010-03-15 12:31:16.223 GMT" R) as.POSIXct("2010-03-15 12:37:17.223",tz="GMT") #very slow [1] "2010-03-15 12:31:16.223 GMT" Now, say I have a file with datetimes expressed in "America/Montral" timezone, the plan is to load them

How to create date from datetime (using lubridate)?

天涯浪子 提交于 2019-12-30 18:58:10
问题 Assume I have created a variable containing date and time: a <- ymd_hms("2014-01-01 12:23:34") How do I create another variable that only has the date? That is, what should I do to transform a 's value to be identical to b 's value where b is b <- ymd("2014-01-01") 回答1: You can just use round_date round_date(a, "day") [1] "2014-01-02 UTC" EDIT You do need to be careful with rounding the time though. For complete equivalence here you would need to use floor_date . identical(ymd("2014-01-01"),

How to create date from datetime (using lubridate)?

折月煮酒 提交于 2019-12-30 18:57:25
问题 Assume I have created a variable containing date and time: a <- ymd_hms("2014-01-01 12:23:34") How do I create another variable that only has the date? That is, what should I do to transform a 's value to be identical to b 's value where b is b <- ymd("2014-01-01") 回答1: You can just use round_date round_date(a, "day") [1] "2014-01-02 UTC" EDIT You do need to be careful with rounding the time though. For complete equivalence here you would need to use floor_date . identical(ymd("2014-01-01"),

Extracting 2 digit hour from POSIXct in R

夙愿已清 提交于 2019-12-30 11:04:09
问题 I would like to extract the hour from a POSIXct time in R, but retrieve the 2 digit answer. For example, test=as.POSIXct("2015-03-02 03:15:00") test [1] "2015-01-02 03:15:00 GMT" month(testing) [1] 1 hour(testing) [1] 3 The results give the relevant month and hour, but I would like to see 01 and 03 instead of just 1 and 3 . 回答1: Try to do this: strftime(test, format="%H") to extract hours and strftime(test, format="%m") for month 来源: https://stackoverflow.com/questions/35227648/extracting-2

Converting to Local Time in R - Vector of Timezones

霸气de小男生 提交于 2019-12-30 10:06:55
问题 I have a set of data from across the US that I am trying to convert into local time for each "subject". I have UTC timestamps on each event and have converted those into POSIXct format, but every time I try to include a vector of tz = DS$Factor or tz = as.character(DS$Factor) in any of the POSIXct/POSIXlt functions (including format() and strftime() ) I get an error that says: Error in as.POSIXlt.POSIXct(x, tz = tz) : invalid 'tz' value If I just enter tz = 'US/Eastern' it works fine, but of

Valid time zones in lubridate

时光毁灭记忆、已成空白 提交于 2019-12-30 07:55:08
问题 A quick google search seems to get me nowhere. What are valid time zones in lubridate's tz option? In particular, am looking for Brasilia's time zone. Thanks! library(lubridate) dts <- c("6-3-1995 12:01:01","29-3-1995 23:01:01","29-3-1995 20:01:01") dmy_hms(dts) # locale's tz default dmy_hms(dts, tz = "chile") # Chilean time (has one time zone only) 回答1: Take a search through OlsonNames() which provides a list of all the valid timezones on the host system. e.g.: grep("Brazil",OlsonNames()

Valid time zones in lubridate

那年仲夏 提交于 2019-12-30 07:55:07
问题 A quick google search seems to get me nowhere. What are valid time zones in lubridate's tz option? In particular, am looking for Brasilia's time zone. Thanks! library(lubridate) dts <- c("6-3-1995 12:01:01","29-3-1995 23:01:01","29-3-1995 20:01:01") dmy_hms(dts) # locale's tz default dmy_hms(dts, tz = "chile") # Chilean time (has one time zone only) 回答1: Take a search through OlsonNames() which provides a list of all the valid timezones on the host system. e.g.: grep("Brazil",OlsonNames()

Adding time to POSIXct object in R

夙愿已清 提交于 2019-12-29 03:32:09
问题 I would like to add 1 hour to a POSIXct object, but it does not support '+'. This command: as.POSIXct("2012/06/30","GMT") + as.POSIXct(paste(event_hour, event_minute,0,":"), ,"%H:%M:$S") returns this error: Error in `+.POSIXt`(as.POSIXct("2012/06/30", "GMT"), as.POSIXct(paste(event_hour, : binary '+' is not defined for "POSIXt" objects How can I add a few hours to a POSIXct object ? 回答1: POSIXct objects are a measure of seconds from an origin, usually the UNIX epoch (1st Jan 1970). Just add

as.POSIXct with datetimes including midnight

对着背影说爱祢 提交于 2019-12-29 01:49:09
问题 I am wanting to convert date-times stored as characters to date-time objects. However if a date time includes midnight then the resulting datetime object excludes the time component, which then throws an error when used in a later function (not needed here - but a function that extracts weather data for specified location and date-time). Example code: example.dates <- c("2011-11-02 00:31:00","2011-11-02 00:00:00","2011-11-02 00:20:22") posix.dates <- as.POSIXct(example.dates, tz="GMT", format