r - trying to compare POSIXct objects in if statements -


i have within function:

x <- as.posixct((substr((dataframe[z, ])$variable, 1, 8)), tz = "gmt",  format = "%h:%m:%s") print(x) if ( (x >= as.posixct("06:00:00", tz = "gmt", format = "%h:%m:%s")) &    (x < as.posixct("12:00:00", tz = "gmt", format = "%h:%m:%s")) ){ position <- "first" } 

but output:

character(0) error in if ((as.numeric(departure) - as.numeric(arrival)) < 0) { : argument of length zero

how can fix comparison works , prints correct thing?

some examples of dataframe$variable column: 16:33:00 15:34:00 14:51:00 07:26:00 05:48:00 11:10:00 17:48:00 06:17:00 08:22:00 11:31:00

welcome stack overflow!

first, reason you've gotten down votes because haven't given in question go on. 1 thing, haven't shown

(dataframe[z, ])$variable 

is, makes hard formulate complete answer. seem trying extract single value dataframe, right? if so, i've never seen done way, try replacing above with:

dataframe$variable[z] 

my guess you're trying achieve comparison of entire column of dataframe called "variable", since that's more useful...

having said that, come against issues time data, , i've heard, experiences not uncommon. when i'm dealing times, appears here, prefer chron::times format on posixct (posix date-time format, date included, tries correct timezone changes, daylight savings changes, tends in way more help). if you've got data in format you've specified in first as.posixct call, won't need specify in calling times function instead.

x <- chron::times( dataframe$variable ) print(x) position <- ifelse ( x >= chron::times( "06:00:00" ) &                      x < chron::times( "12:00:00" ),                 "first", "not first"  ) 

this output vector "position", result values taken dataframe$variable. achieve you're hoping for?

from here, if did want extract comparison result particular row "z" in dataframe, can still with

position[z] 

edit add: might worth checking missing values in "variable". should return true:

sum( is.na( dataframe$variable ) ) == 0 

also check aren't correctly formatted. again, should return true:

sum( is.na( chron::times( dataframe$variable ) ) ) == 0 

edit add: per comments, looks values in "variables" column aren't converting properly. should able find them with

subset( dataframe, is.na( chron::times( variable ) ) ) 

that should let see what's wrong. may single cell, or may number of them. you'll need tidy data, can in few ways. go through , fix them manually, add function in script repair them before conversion (this might idea if there common issue between of values, or if expect same issue happen again new data comes in, if indeed need allow that).

the other option exclude rows analysis. if go route, make sure it's appropriate analysis you're running. if appropriate in case, can add step clean dataframe before running steps in question:

dataframe <- subset( dataframe, !is.na( chron::times( variable ) ) ) 

note: there's chance come warning. if run same line twice, , warning goes away second time (after offending rows have been removed), may need further it.

that should drop offending values, leaving values converting times format, should steps you're trying run. check how dataframe dimensions change before , after step; that'll tell how many rows you're dropping.

you same thing posixct if that's you're comfortable with, i'm more comfortable times you're doing.


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -