-1

I have a data frame which is a time series of meteorological measurement with monthly resolution from 1961 till 2018. I am interested in the variable that measures the monthly average temperature since I need the multi-annual average temperature for the summers.

To do this I must filter from the "DateVaraible" column the fifth and sixth digit, which are the month. The values in time column are formatted like this "19610701". So I need the 07(Juli) after 1961.

I start coding for 1 month for other purposes, so I did not try anything worth to mention. I guess that .grepl could do the work, but I do not know how the "matching" operator works.

So I started with this code that works.

summersmonth<- Df[DateVariable %like% "19610101" I DateVariable %like% "19610201"]

I am expecting a code like this

summermonths <- Df[DateVariable %like% "**06**" I DateVariable%like% "**07**..]

So that all entries with month digit from 06 to 09 are saved in the new dataframe summermonths.

Thanks in advance for any reply or feedback regarding my question.

Update

Thank to your answers I got the first part, which is to convert the variable in a as.date with the format "month"(Class=char) Now I need to select months from Juni to September . A horrible way to get the result I wanted is to do several subset and a rbind afterward.

Sommer1<-subset(Df, MonthVar == "Mai")
Sommer2<-subset(Df, MonthVar == "Juli")
Sommer3<-subset(Df, MonthVar == "September")

SummerTotal<-rbind(Sommer1,Sommer2,Sommer3)

I would be very glad to see this written in a tidy way.

Update 2 - Solution

Here is the tidy way, as here Using multiple criteria in subset function and logical operators

Veg_Seas<-subset(Df, subset = MonthVar %in% c("Mai","Juni","Juli","August","September"))
m_rub
  • 31
  • 6
  • If `DateVariable` always follows the format: "YYYYMMDD", you could use `substr`: `summermonths <- Df[substr(DateVariable, 6, 6) %in% 6:9, ]` – Andrew Jul 02 '19 at 13:13
  • re your edit, is `Mean_Temp$MESS_DATUM_BEGINN` the result of `month` function on your date variable (converted into date)? – Cath Jul 03 '19 at 07:16
  • Yes, Mean_Temp$MESS_DATUM_BEGINN is the result of the function.(allmonths) I first tried your function but it didnt work beacuse of the missing **s** in `month`, therefore I used `as.Date` withouth `month` which gave me a result in this format _1961-01-01_. Today I tried again your function with `months`. The result is a character class with the name of the month. Now I would like to know how to subset them, either as char. or num. value – m_rub Jul 03 '19 at 15:17

2 Answers2

7

You can convert your date variable as date (format) and take the month:

allmonths <- month(as.Date(Df$DateVariable, format="%Y%m%d"))

Note that of your column has been originally imported as factor you need to convert it to character first:

allmonths <- month(as.Date(as.character(Df$DateVariable), format="%Y%m%d"))

Then you can check whether it is a summermonth:

summersmonth <- Df[allmonths %in% 6:9, ]

Example:

as.Date("20190702", format="%Y%m%d")
[1] "2019-07-02"

month(as.Date("20190702", format="%Y%m%d"))
[1] 7
Cath
  • 23,906
  • 5
  • 52
  • 86
  • Hi, I tried the first line of your code(conversion to as.Date) and I got this message: "Error in as.Date.numeric(Mean_Temp$MESS_DATUM_BEGINN, format = "%Y%m%d") : 'origin' must be supplied ". – m_rub Jul 02 '19 at 16:14
  • @m_rub please give the result of `dput(head(Mean_Temp$MESS_DATUM_BEGINN))` : without further details it's hard to know what causes the error. It's very likely that you are dealing with `factors` and not `characters`, then passing the same line with `allmonths <- month(as.Date(as.character(Df$DateVariable), format="%Y%m%d"))` should do the trick – Cath Jul 03 '19 at 06:34
0

We can use anydate from anytime to convert to Date class and then extract the month

library(anytime)
month(anydate(as.character(Df$DateVariable)))
akrun
  • 874,273
  • 37
  • 540
  • 662
  • @m_rub May be you have numeric values. Convert to `character` and try. I updated the post – akrun Jul 02 '19 at 17:37
  • True, as I found out here: https://stackoverflow.com/questions/24315463/how-to-determine-the-correct-argument-for-origin-in-as-date-r Thanks! – m_rub Jul 02 '19 at 17:44
  • 1
    Hi, I appreciate you help and I am willing to give you the credit, but my initial question was more about how to filter/subset some month, than how to change format. I guess change format could be the first step to get my problem solved, but the problem itself is still unsolved. – m_rub Jul 02 '19 at 18:25