POSIXct substitution for odd or even seconds

I would like a subset of the data frame to save only the observations where the seconds are an even number.

You can upload a small portion of my data here (100 lines).

The first 6 lines are as follows:

            Timestamp C01 C02 C03 C04 C05 C06 C07 C08 C09 C10 C11 C12 C13 C14
1 2013-04-01 00:00:00   0   1   1   1   1   0   1   1   1   1   0   1   0   1
2 2013-04-01 00:00:01   0   1   1   1   1   0   1   1   1   1   0   1   0   1
3 2013-04-01 00:00:02   0   1   1   1   1   0   1   1   1   1   0   1   0   1
4 2013-04-01 00:00:03   0   1   1   1   1   0   1   1   1   1   0   1   0   1
5 2013-04-01 00:00:04   0   1   1   1   1   0   1   1   1   1   0   1   0   1
6 2013-04-01 00:00:05   0   1   1   1   1   0   1   1   1   1   0   1   0   1

And I would like it to look like this:

            Timestamp C01 C02 C03 C04 C05 C06 C07 C08 C09 C10 C11 C12 C13 C14
1 2013-04-01 00:00:00   0   1   1   1   1   0   1   1   1   1   0   1   0   1
2 2013-04-01 00:00:02   0   1   1   1   1   0   1   1   1   1   0   1   0   1
3 2013-04-01 00:00:04   0   1   1   1   1   0   1   1   1   1   0   1   0   1
4 2013-04-01 00:00:06   0   1   1   1   1   0   1   1   1   1   0   1   0   1
5 2013-04-01 00:00:08   0   1   1   1   1   0   1   1   1   1   0   1   0   1
6 2013-04-01 00:00:10   0   1   1   1   1   0   1   1   1   1   0   1   0   1

I understand how a subset of the time intervals is from here and here , but I could not find an example similar to my question, and honestly, I do not know where to start.

Thanks!

Note. The Timestamp variable is already formatted in POSIXct.

+4
source share
4 answers

( )

res =  df[(as.numeric(substr(df$Timestamp, 18, 19)) %% 2) == 0,]

:

df = data.frame(Timestamp = c("2013-04-01 00:00:00", "2013-04-01 00:00:01", "2013-04-01 00:00:02", "2013-04-01 00:00:03", "2013-04-01 00:00:04"), C01 = rep(0,5), C02 = rep(1,5))
df$Timestamp = as.POSIXct(df$Timestamp)

( ):

#> res
#            Timestamp C01 C02
#1 2013-04-01 00:00:00   0   1
#3 2013-04-01 00:00:02   0   1
#5 2013-04-01 00:00:04   0   1

, ==0 ==1

+1

, , , , POSIXct, , ( 1- 1970 ), ( , , ,...):

a <- seq(as.POSIXct("2013-04-01 00:00:00"),as.POSIXct("2013-04-01 01:00:00"),by="secs")
a[as.integer(a)%%2==0]
+5
library(lubridate)

foo <- seq(as.POSIXct("2013-01-10"), as.POSIXct("2013-01-11"), by = "secs")

secs <- second(foo)

even <- foo[secs %% 2 == 0]
odd <- foo[secs %% 2 == 1]

, , data.frame .

+4

A base :

tt <- c(Sys.time(), Sys.time() + 1)
tt
# [1] "2013-10-29 19:43:26 CET" "2013-10-29 19:43:27 CET"

tt[as.numeric(format(tt, "%S")) %% 2 == 0]
# [1] "2013-10-29 19:43:26 CET"

Update with faster alternative thanks to @Roland

tt[round(as.POSIXlt(tt)$sec) %% 2 == 0]
+4
source

All Articles