0

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!

  • 2
    All the rows seem to be in the same format, but if you just want rows for that specific date you can use: `dplyr::filter(df, V1 == "Wed", V2 == "Sep", V3 == "15")` – austensen Oct 14 '18 at 04:45
  • Well I did follow the above mentioned procedure, bu it didn't work for me. I have read this csv file using read.table(). – user9815591 Oct 14 '18 at 05:07
  • 1
    it'll be easy for folks to help if you include a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) along with your question – austensen Oct 14 '18 at 05:17
  • Also, try `V3 == 15` instead of `V3 == "15"` in austensen's suggestion. – Mankind_008 Oct 14 '18 at 05:19
  • Can you post a dput of your dataframe. See [Example of using dput()](https://stackoverflow.com/questions/49994249/example-of-using-dput) for more info on how to use. – Russ Thomas Oct 14 '18 at 05:20
  • What function did you use to read your file? – krads Oct 14 '18 at 09:12

1 Answers1

0

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')
Randall Helms
  • 849
  • 5
  • 15