I have a dataframe which has a date column, a column of ints (labelled value in the example below), and 12 other numeric columns, each corresponding to a month and labelled X1 (jan) through X12 (dec).
It looks something like:
date_var value X1 X2 X3 ... X12
2016-01-01 100 1212 4161 9080 ... 383
2016-02-01 150 1212 4161 9080 ... 383
2016-03-01 150 1212 4161 9080 ... 383
What I'd like to do is create a new column, lets call it Z, which corresponds to the number in the value column, divided by the appropriate monthly value.
For example, in the table above Z for the 2016-01-01 entry would equal 100/1212, whereas the 2016-02-01 entry would instead divide by X2 for Feb and 2016-03-01 would have value divided by X3:
date_var value X1 X2 X3 ... X12 Z
2016-01-01 100 1212 4161 9080 ... 383 0.0825
2016-02-01 150 1212 4161 9080 ... 383 0.0360
2016-03-01 150 1212 4161 9080 ... 383 0.0165
I've tried various approaches along the lines of attempting to divide value by df[paste("X", month(df$date_var), sep = '')], although this returned list a rather than working element-wise so obviously isn't the correct approach.