0

I have a sequence of dates in R with 2 months.

Example:

2020-01-28 / 2020-01-29 / 2020-01-30 / 2020-01-31 / 2020-02-01 / 2020-02-02

I need to extract the dates with the first month, in that case, January. But, I'm not able to do this.

I already tried to use subset, but didn't work.

  • Welcome to SO! Your example is unclear. Can you share an R object via `dput()` (paste it in as a code)? – s_baldur Jan 29 '20 at 15:05
  • Is this a string? a list? a matrix? It is a good practice to provide a [minimum reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). It improves your chance to get an answer. – rvbarreto Jan 29 '20 at 15:19

2 Answers2

2

You need to get a vector with the month value. Here is an example, assuming that your data are in a vector like I did :

date = c("2020-01-28","2020-01-29","2020-01-30","2020-01-31","2020-02-01","2020-02-02")
library(lubridate)
month(date) # get the month value only
# [1] 1 1 1 1 2 2
january <- date[month(date)==1] # extract only the date which are in the 1st month
january
# [1] "2020-01-28" "2020-01-29" "2020-01-30" "2020-01-31"
Gowachin
  • 1,251
  • 2
  • 9
  • 17
1

Maybe you can try the code below

r <- subset(v,format(as.Date(v),"%b") == "Jan")

DATA

v <- c("2020-01-28", "2020-01-29", "2020-01-30", "2020-01-31", "2020-02-01", "2020-02-02")
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81