Complete.cases and data.frames with POSIXlt

I am primarily a Python programmer by profession, but I'm still studying R. I have a data problem here , but I will give a small example of the problem below.

Function

R complete.cases()should return a logical vector indicating which rows of the test object (s) do not contain NA values.

df <- data.frame(val1=c(1, 2, NA, 4),
                 val2=c("12:00", "10:30", "09:15", "05:00"))
print(df)
##   val1  val2
## 1    1 12:00
## 2    2 10:30
## 3   NA 09:15
## 4    4 05:00

print(length(df$val1) == length(df$val2))
## [1] TRUE

complete.cases(df)
## [1]  TRUE  TRUE FALSE  TRUE

This works as expected: one value Falsecorresponding to value NAin df$val1.


I ran into a problem when after converting a character vector to a vector POSIXltthrough strptime, it complete.casesno longer works due to the following error:

Error in complete.cases(df) : not all arguments have the same length.

For instance:

df$val2 <- strptime(df$val2, format="%H:%M")
print(df)
##   val1                val2
## 1    1 2015-01-14 12:00:00
## 2    2 2015-01-14 10:30:00
## 3   NA 2015-01-14 09:15:00
## 4    4 2015-01-14 05:00:00

print(length(df$val1) == length(df$val2))
## [1] TRUE

complete.cases(df)  # This line now causes the error.

, df$val1 df$val2 . - ? complete.cases , strptime?

, ( , strptime), , complete.cases .


:

  • R 3.1.1 (2014-07-10) - " "
  • R Studio Version 0.98.1087
  • Windows 7
+4
1

strptime() POSIXlt, . , .

df <- data.frame(
    val1 = c(1, 2, NA, 4),
    val2 = c("12:00", "10:30", "09:15", "05:00")
)
df$val2 <- strptime(df$val2, format="%H:%M")
is.list(df$val2)
# [1] TRUE

. complete.cases() , na.omit(df) POSIXlt.


@BondedDust, POSIXlt , . POSIXct, , complete.cases() .

df ...

df <- data.frame(
    val1 = c(1, 2, NA, 4),
    val2 = c("12:00", "10:30", "09:15", "05:00")
)
df$val2 <- as.POSIXct(df$val2, format="%H:%M")
complete.cases(df)
# [1]  TRUE  TRUE FALSE  TRUE

, unclass() df$val2, .

+3

All Articles