简体   繁体   English

仅提取与R中日期的特定子字符串匹配的值

[英]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. 请注意,此数据位于.csv文件中,我想提取日期为Wed Sep 15格式的行,如何执行此操作。 Please clarify this issue as I'm new to R. Thanks! 由于我是R新手,请澄清此问题。谢谢!

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 我将假设您的数据框称为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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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