User981636
User981636

Reputation: 3629

R Date not in character list

I have a data frame with several variables, among those, a date (df$date) in Date format.

I also have list with dates, in character format (e.g. "2013-07-14"). I need to remove the list dates from the data frame.

I have tried the following but none of them is working.

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)) 

Data:

    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"))

Upvotes: 1

Views: 63

Answers (1)

NicE
NicE

Reputation: 21435

You can try running this, without transforming the list to characters:

df[!(df$date %in% datestoremove[[1]]),]

Upvotes: 2

Related Questions