R 日期不在字符列表中
R Date not in character list
我有一个包含多个变量的数据框,其中有一个日期格式的日期 (df$date)。
我还有日期列表,采用字符格式(例如“2013-07-14”)。我需要从数据框中删除列表日期。
我尝试了以下方法,但其中 none 有效。
datestoremove <- as.character(datestoremove) #from list to character format
require(lubridate)
test1 <- ymd(datestoremove) #yields NA
grepl(df$date[3600],datestoremove) # Try to identify character pattern. Doesn't work as it yields FALSE even when it is true (I know df$date[3600] is in the list.
datestoremove <- strptime(datestoremove, format = "%Y-%m-%d") # Passing datestoremove to date format. Unsucessfully.
test1 <- strptime(datestoremove,"%Y-%m-%d")
test2 <- df[which(!(df$date[3600] %in% datestoremove)),] #Seems to work but it does not transfer the valid rows to test2. So test2 is empty.
test3 <- subset(df, !(df$date %in% datestoremove))
数据:
df <- structure(list(date = structure(c(15930, 15931, 15931, 15931,
15931, 15931), class = "Date"), st1count = c(259L, 4L, 36L, 54L,
67L, 56L)), .Names = c("date", "st1count"), class = "data.frame", row.names = 3420:3425)
datestoremove <- list(structure(c(15931, 15979, 16252, 16322), class = "Date"))
您可以尝试 运行 这个,而不将列表转换为字符:
df[!(df$date %in% datestoremove[[1]]),]
我有一个包含多个变量的数据框,其中有一个日期格式的日期 (df$date)。
我还有日期列表,采用字符格式(例如“2013-07-14”)。我需要从数据框中删除列表日期。
我尝试了以下方法,但其中 none 有效。
datestoremove <- as.character(datestoremove) #from list to character format
require(lubridate)
test1 <- ymd(datestoremove) #yields NA
grepl(df$date[3600],datestoremove) # Try to identify character pattern. Doesn't work as it yields FALSE even when it is true (I know df$date[3600] is in the list.
datestoremove <- strptime(datestoremove, format = "%Y-%m-%d") # Passing datestoremove to date format. Unsucessfully.
test1 <- strptime(datestoremove,"%Y-%m-%d")
test2 <- df[which(!(df$date[3600] %in% datestoremove)),] #Seems to work but it does not transfer the valid rows to test2. So test2 is empty.
test3 <- subset(df, !(df$date %in% datestoremove))
数据:
df <- structure(list(date = structure(c(15930, 15931, 15931, 15931,
15931, 15931), class = "Date"), st1count = c(259L, 4L, 36L, 54L,
67L, 56L)), .Names = c("date", "st1count"), class = "data.frame", row.names = 3420:3425)
datestoremove <- list(structure(c(15931, 15979, 16252, 16322), class = "Date"))
您可以尝试 运行 这个,而不将列表转换为字符:
df[!(df$date %in% datestoremove[[1]]),]