jpinelo
jpinelo

Reputation: 1454

select rows by element components of timestamp

I have a vector made up of timestamps as POSIXlt, format: "2015-01-05 15:00:00, which I extracted from a timeframe.

I want to reassign the vector by loosing all elements where Minutes != 00

I've tried

vector <- vector[format(vector, "%M") == 00,]

which creates the following error of missing argument

Error in lapply(X = x, FUN = "[", ..., drop = drop) : argument is missing, with no default

Also tried

vector <- vector["%M""== 00]

Which is seems to be an open command

Since POSIX time is stored as number of elapsed seconds since 1 Jan 1970, I guess that I could do this by excluding from my vector all elements which are not multiple of 3600. I rather not use this approach though. Thank you in advance, I'm new to R.

Upvotes: 1

Views: 82

Answers (3)

jpinelo
jpinelo

Reputation: 1454

Using:

vector2 <- vector2[v2$min==0]

I reassign vector 2 (v2) excluding all elements where minutes are not 0. This was suggested by @akrun. It does the selection while keeping data type as POSIX.

There were two issues with the first option of initial code:

1.function format() returns character;

2.there was a "," before last "]", which meant that the function was expecting another argument, which does not make sense to a vector as explained by @balint.

With the second option initially submitted there were a few syntax mistakes. The correct syntax is that on this answer, as suggested by @akron.

Upvotes: 0

akrun
akrun

Reputation: 887118

You could try

v2[!v2$min]
#[1] "2015-01-05 15:00:00 EST" "2015-01-05 15:00:30 EST"

Or your command should also work without the comma

data

v1 <- c("2015-01-05 15:00:00", "2015-01-05 15:45:00", "2015-01-05 15:00:30")
v2 <- strptime(v1, '%Y-%m-%d %H:%M:%S')

Upvotes: 1

Balint Domokos
Balint Domokos

Reputation: 1021

Format returns a character type, not numeric, so you should compare it to "00". Also the comma is not needed, as there's only 1 dimension.

vector <- vector[format(vector, "%M") == "00"]

Upvotes: 1

Related Questions