posixct

How to prevent write.csv from changing POSIXct, dates and times class back to character/factors?

妖精的绣舞 提交于 2019-12-01 01:18:52
I have a .csv file with one field each for datetime, date and time. Originally they are all character fields and I have converted them accordingly. At the end of my code, if I do: str(data) I will get datetime: POSIXct date: Date time: Class 'times' atomic [1:2820392] (....) attr(*, "format")= chr "h:m:s" Now, I am very happy with this and I want to create a .csv file, so this is what I have: write.csv(data, file = "data.csv", row.names = FALSE) I have also tried write.table(data, "data.csv", sep = ",", row.names = FALSE) And I get the same result with both, which is all my convertion gets

Subsetting based on co-occurrence within a time window

人走茶凉 提交于 2019-11-30 21:20:15
问题 I am having trouble subsetting data based on different attributes in different columns. Here is a dummy data set with species, area where it was found, and time (already in POSIXct). SP Time Area B 07:22 1 F 09:22 4 A 09:22 1 C 08:17 3 D 09:20 1 E 06:55 4 D 09:03 1 E 09:12 2 F 09:45 1 B 09:15 1 I need to subset the rows that have SP==A, plus all other species occurring in the same area (in this case 1), within a time window of +30 and -30 minutes returning this: SP Time Area A 09:22 1 D 09:20

Converting chr “00:00:00” to date-time “00:00:00”

心已入冬 提交于 2019-11-30 17:38:18
问题 My question comes from this question. The question had the following character string. x <- "2007-02-01 00:00:00" y <- "02/01/2007 00:06:10" If you try to convert this string to date-class object, something funny happens. This is a sample from @nrusell's answer. as.POSIXct(x,tz=Sys.timezone()) [1] "2007-02-01 EST" as.POSIXct(y,format="%m/%d/%Y %H:%M:%S",tz=Sys.timezone()) [1] "2007-02-01 00:06:10 EST" As you see, 00:00:00 disappears from the first example. @Richard Scriven left the following

Join two data frames in R based on closest timestamp

醉酒当歌 提交于 2019-11-30 12:17:53
问题 Hi I have two tables (table1 and table2 below) and would like to join them based on the closest timestamp to form expected_output. Some kind of solution involving dplyr would be great if possible, but not if it further complicates things. table1 = structure(list(date = structure(c(1437051300, 1434773700, 1431457200 ), class = c("POSIXct", "POSIXt"), tzone = ""), val1 = c(94L, 33L, 53L)), .Names = c("date", "val1"), row.names = c(NA, -3L ), class = "data.frame") table2 = structure(list(date =

rounding times to the nearest hour in R [duplicate]

旧时模样 提交于 2019-11-30 12:04:00
This question already has an answer here: Round a POSIX date and time (posixct) to a date relative to a timezone 1 answer I have data in the format time <- c("16:53", "10:57", "11:58") etc I would like to create a new column where each of these times is rounded to the nearest hour. I cannot seem to get the POSIX command to work for me. as.character(format(data2$time, "%H:%M")) Error in format.default(structure(as.character(x), names = names(x), dim = dim(x), : invalid 'trim' argument Let alone use the round command. Can anyone advise? ## Example times x <- c("16:53", "10:57", "11:58") ## POSIX

as.POSIXct gives an unexpected timezone

旧街凉风 提交于 2019-11-30 08:06:35
问题 I'm trying to convert a yearmon date (from the zoo package) to a POSIXct in the UTC timezone. This is what I tried to do: > as.POSIXct(as.yearmon("2010-01-01"), tz="UTC") [1] "2010-01-01 01:00:00 CET" I get the same when I convert a Date: > as.POSIXct(as.Date("2010-01-01"),tz="UTC") [1] "2010-01-01 01:00:00 CET" The only way to get it to work is to pass a character as an argument: > as.POSIXct("2010-01-01", tz="UTC") [1] "2010-01-01 UTC" I looked into the documentation of DateTimeClasses,

How to add/subtract time from a POSIXlt time while keeping its class in R?

て烟熏妆下的殇ゞ 提交于 2019-11-30 06:42:30
I am manipulating some POSIXlt DateTime objects. For example I would like to add an hour: my.lt = as.POSIXlt("2010-01-09 22:00:00") new.lt = my.lt + 3600 new.lt # [1] "2010-01-09 23:00:00 EST" class(new.lt) # [1] "POSIXct" "POSIXt" The thing is I want new.lt to be a POSIXlt object. I know I could use as.POSIXlt to convert it back to POSIXlt , but is there a more elegant and efficient way to achieve this? Short answer: No Long answer: POSIXct and POSIXlt objects are two specific types of the more general POSIXt class (not in a strictly object oriented inheritance sense, but in a quasi-object

Get the month from the week of the year

我与影子孤独终老i 提交于 2019-11-29 15:45:36
Let's say we have this: ex <- c('2012-41') This represent the week 41 from the year 2012. How would I get the month from this? Since a week can be between two months, I will be interested to get the month when that week started (here October). Not duplicate to How to extract Month from date in R (do not have a standard date format like %Y-%m-%d). The following will add the week-of-year to an input of year-week formatted strings and return a vector of dates as character. The lubridate package weeks() function will add the dates corresponding to the end of the relevant week. Note for example I

Round a POSIX date and time (posixct) to a date relative to a timezone

冷暖自知 提交于 2019-11-29 14:42:54
I want to round a POSIXct down to the day, relative to a specific timezone. If I try round(as.POSIXct("2013-03-05 23:00:00 EST"), "day") It returns 2013-03-06 Which makes sense, in that when it's 23:00:00 EST on 2013-03-05 in EST5EDT, it's already 2013-03-06 in UTC. Logically, what I want to do is: round(as.POSIXct("2013-03-05 23:00:00 EST"), "day", tz="EST5EDT") That is, "round this date and time to the nearest day, relative to the EST5EDT time zone". Unfortunately, round doesn't take a time zone parameter. round will round to the next day once it's past midday, which is why I think you are

Subset observations that differ by at least 30 minutes time

*爱你&永不变心* 提交于 2019-11-29 11:35:24
问题 I have a data.table (~30 million rows) consisting of a datetime column in POSIXct format, an id column and a few other columns (in the example, I just left one irrelevant column x to demonstrate that there are other columns present that need to be kept). A dput is at the bottom of the post. head(DT) # datetime x id #1: 2016-04-28 16:20:18 0.02461368 1 #2: 2016-04-28 16:41:34 0.88953932 1 #3: 2016-04-28 16:46:07 0.31818101 1 #4: 2016-04-28 17:00:56 0.14711365 1 #5: 2016-04-28 17:09:11 0