简体   繁体   中英

Extract only the values that match a specific substring of a date in R

Suppose I have a data set which looks as follows:

    V1  V2 V3    V4     V5   V6
    Tue Aug 10 10:04:09  0 2018
    Thu Aug 12 10:05:34  0 2018
    Wed Sep 15 09:25:56  0 2018
    Wed Sep 15 12:37:29  0 2018
    Mon Oct 17 04:21:18  0 2018
    Tue Oct 18 12:45:38  0 2018

Note that this data is in a .csv file and I want to extract the rows where the date is in the format Wed Sep 15 how to do this. Please clarify this issue as I'm new to R. Thanks!

Maybe the easiest way to do what you want, assuming that you want to do this on an ongoing basis, is to create a combined date column, then use that to filter. I will assume that your data frame is called df

library(dplyr)

df <- df %>%
    mutate(full_date = paste(V1,V2,V3))

#filter to your date
filt_date <- df %>%
    filter(full_date == 'Wed Sep 15')

If this is going to be a regular thing that you want to do, then you can also make a simple function, like this:

date_filter <- function(x,date) {

    y <- x %>%
        filter(full_date == date)

    return(y)
}

Then in the example above, you would do this:

filt_date <- df %>%
    date_filter('Wed Sep 15')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM