1

I was working on RStudio with some data. I have sales data of a product month wise and the warranty expenses incurred for it monthly. Over the years, the months were entered in various nomenclatures making it hard for me to filter the data according to month. The data is as follows:

Month
Apr
APR-13.
APR-14.
APR-15.
Aug
AUG-12.
AUG-13.
AUG-14.
AUG-15.
Dec
DEC-12.
DEC-13.
DEC-14.
DEC-15.
Feb
FEB-13.
FEB-14.
FEB-15.
Jan
JAN-07.
JAN-13.
JAN-14.
JAN-15.
Jul
JUL-12.
JUL-13.
JUL-14.
JUL-15.
....
....

I wanted to subset the above data according to the months.

I tried to use the subset function of the R.

subset(SR,SR$Month=='Apr')

The above code is giving only the results of the Month 'Apr' instead of giving the data of all months which contain Apr.

Ronak Shah
  • 286,338
  • 16
  • 97
  • 143
  • 1
    Use `grepl` instead. You might also want to use `ignore.case` parameters to ignore the case. `subset(df, grepl("Apr", Month, ignore.case = TRUE))` – Ronak Shah Jan 10 '19 at 05:51
  • Your data is illegible, and I can't really read your question. Please format your question to that other may read it. Also, you probably don't need to give us 30-40 points of data to get your question across, just 5-10 points would do. – Tim Biegeleisen Jan 10 '19 at 05:52
  • The best way to subset a data frame based on part of the string in a column is to use `dplyr` in combination with `stringr`: `SR % dplyr::filter(stringr::str_detect(Month,"(?i)apr)")` – José Jan 10 '19 at 10:32
  • Anyway I think you should edit your question to make your code reproducible. – José Jan 10 '19 at 10:34

0 Answers0