I have one data frame which has sales values for the time period Oct. 2000
to Dec. 2001
(15 months). Also I have profit values for the same time period as above and I want to find the correlation between these two data frames month wise for these 15 months in R
. My data frame sales
is:
Month sales
Oct. 2000 24.1
Nov. 2000 23.3
Dec. 2000 43.9
Jan. 2001 53.8
Feb. 2001 74.9
Mar. 2001 25
Apr. 2001 48.5
May. 2001 18
Jun. 2001 68.1
Jul. 2001 78
Aug. 2001 48.8
Sep. 2001 48.9
Oct. 2001 34.3
Nov. 2001 54.1
Dec. 2001 29.3
My second data frame profit
is:
period profit
Oct 2000 14.1
Nov 2000 3.3
Dec 2000 13.9
Jan 2001 23.8
Feb 2001 44.9
Mar 2001 15
Apr 2001 58.5
May 2001 18
Jun 2001 58.1
Jul 2001 38
Aug 2001 28.8
Sep 2001 18.9
Oct 2001 24.3
Nov 2001 24.1
Dec 2001 19.3
Now I know that for initial two months I cannot get the correlation as there are not enough values but from Dec 2000
onwards I want to calculate the correlation by taking into consideration the previous months values. So, for Dec. 200
I will consider values of Oct. 2000
, Nov. 2000
and Dec. 2000
which will give me 3 sales value and 3 profit values. Similarly for Jan. 2001
I will consider values of Oct. 2000
, Nov. 2000
Dec. 2000
and Jan. 2001
thus having 4 sales value and 4 profit value. Thus for every month I will consider previous month values also to calculate the correlation and my output should be something like this:
Month Correlation
Oct. 2000 NA or Empty
Nov. 2000 NA or Empty
Dec. 2000 x
Jan. 2001 y
. .
. .
Dec. 2001 a
I know that in R
there is a function cor(sales, profit)
but how can I find out the correlation for my scenario?
cor(sales[1:i], profit[1:i])
for increasingi
? – Spacedman?ccf
)? – RolandOct 2000
Nov. 2000
Dec 2001
uptillDec 2001
. For the first two months(Oct 2000
&` Nov. 2000) I cannot get a correlation as there is only 1 or 2 values on each side of sales and profit. But for
Dec 2000` onwards I can get the correlation as I will be considering previous month values also thus giving 3 values on each forDec 2000
. So for each month I will consider values of all previous months. – user2966197