Search code examples
rdateassigndate-arithmeticr-factor

R - new column with values assigned depending on month


I have a dataframe which contains a date column:

          date  t14  rh14
1   2013-05-01 14.8  56.5
2   2013-05-02 14.5  71.8
3   2013-05-03 17.5  40.3
4   2013-05-04 19.0  34.6
5   2013-05-05 21.4  45.3

in which the month of the date can be extracted (I used the following:)

date=as.Date(paste(df.date$year,df.date$month,df.date$day, sep="-"),format="%Y-%m-%d")

Now I want a new column in my dataframe, which is a factor f depending on the month (January -> first value, etc)

factor=c(0.22,0.22,0.22,0.29,0.29,0.28,0.26,0.25,0.23,0.22,0.22,0.22)

Been looking for a solution for a while and still don't really know where to start. Still pretty new to R and programming in general. Any help would be appreciated a lot. Thanks in advance!


Solution

  • It sounds like you already have month stored as a separate variable in the df.date data frame. If so, you could create a factor from it like so:

    Haude.input$monthF <- factor(df.date$month, levels=1:12, labels=c(0.22,0.22,0.22,0.29,0.29,0.28,0.26,0.25,0.23,0.22,0.22,0.22))
    

    However, your values seem better suited to a numeric variable rather than a factor (in R, 'factor' refers to something categorical). If that is in fact true, you could do:

    values = c(0.22,0.22,0.22,0.29,0.29,0.28,0.26,0.25,0.23,0.22,0.22,0.22)
    Haude.input$monthF <- values[df.date$month]