根据标题,我做一个简单的例子来测试drop_na {tidyr}
:
library(tidyr)
library(dplyr)
# (1.) produce a dataset with two POSIX type "ct" and "lt"
data <- data.frame(n = 1:5)
data$ct <- as.POSIXct(Sys.time() + rnorm(5) * 1000)
data$lt <- as.POSIXlt(Sys.time() + rnorm(5) * 1000)
str(data)
# $ n : int 1 2 3 4 5
# $ ct: POSIXct, format: "2018-10-07 03:02:28" ...
# $ lt: POSIXlt, format: "2018-10-07 02:37:26" ...
# (2.) assign the third values of "ct" and "lt" to NA
data[3, c("ct", "lt")] <- NA
# (3.) use different function to remove rows with NA
data %>% is.na() # identify NAs in both "ct" and "lt"
data %>% drop_na('ct') # drop NA from "ct"
data %>% drop_na('lt') # NOT drop NA from "lt"
data[c(1, 2)] %>% na.omit() # drop NA from "ct"
data[c(1, 3)] %>% na.omit() # NOT drop NA from "lt"
从上面的结论来看,如果 POSIX-lt 变量中有 NA,则只能is.na()
用于删除有 NA 的行。
我大致知道POSIX“ct”和“lt”之间的区别。
POSIXct
将自 1970 年初以来的秒数表示为数值向量。POSIXlt
是表示向量的命名列表。
所以有人可以解释为什么POSIXlt
不能用drop_na()
and来识别缺失值na.omit()
吗?