I am varily new to R and data wrangling. I would like to calculate a correlation matrix using the cor()-function and for that I have to reshape my data frame. It consists of 1500 different articles (StockCodes) of an online store, with a sale history of 365 days per article (Quantity per Date per StockCode).
I need to reshape it in the form that the Dates become the new row names and the StockCodes become the new column names.
Concider this short example:
my_df <- data.frame(Stockcode = c("A","A", "B","B", "C", "C"), Quantity = c(1,5,3,2,1,4), Date = c("2010-12-01","2010-12-02","2010-12-01","2010-12-02","2010-12-01","2010-12-02") )
looking like this:
Stockcode Quantity Date
1 A 1 2010-12-01
2 A 5 2010-12-02
3 B 3 2010-12-01
4 B 2 2010-12-02
5 C 1 2010-12-01
6 C 4 2010-12-02
And I want it to be transformed into:
df_reshaped <- data.frame(A = c(1,5), B = c(3,2), C = c(1,4), row.names = c("2010-12-01","2010-12-02"))
looking like this:
A B C
2010-12-01 1 3 1
2010-12-02 5 2 4
I achieved this with a for-loop (and successfully calculated my correlation matrix), but the loop took "ages" to be executed (approx. 4 hours).
Is there a proper and faster way?
I would highly appreciate any help!