I have a problem with my R dataset.
How can I calculate the correlation with the lapply function between sales and share price – as a quick reference, so to speak. I tried this – but doens’t work:
my_correlation <- function(subset_df) {
subset_correlation <- image(cor(subset_df), x=Sales, y=Stockprice_quarterly)
subset_correlation
}
ss <- lapply(unique(Nasdaq_100$TickerSymbol), function(ticker)
my_correlation(subset(Nasdaq_100, Nasdaq_100$TickerSymbol == ticker)))
This is a sample I created to show the structure of my dataset:
TickerSymbol Quarter Sales Stockprice_quarterly
AMD 31.03.2021 $0.45 502.500
AMD 31.12.2020 $1.47 361.100
AMD 30.09.2020 $0.32 280.700
AMD 30.06.2020 $0.13 377.400
AMD 31.03.2020 $0.14 296.900
AMD 31.12.2019 $0.15 274.800
AMD 30.09.2019 $0.11 561.200
AMD 30.06.2019 $0.03 548.650
AMD 31.03.2019 $0.01 509.977
AAPL 31.03.2021 $1.40 359.038
AAPL 31.12.2020 $1.68 358.514
AAPL 30.09.2020 $0.75 357.991
AAPL 30.06.2020 $0.65 357.467
AAPL 31.03.2020 $0.64 356.944
AAPL 31.12.2019 $1.25 356.421
AAPL 30.09.2019 $0.77 355.897
AAPL 30.06.2019 $0.55 355.374
AAPL 31.03.2019 $0.62 354.851
EBAY 31.03.2021 $0.92 325.020
EBAY 31.12.2020 $1.39 324.496
EBAY 30.09.2020 $0.94 323.973
EBAY 30.06.2020 $1.05 323.449
EBAY 31.03.2020 $4.51 322.926
EBAY 31.12.2019 $0.69 322.403
EBAY 30.09.2019 $0.37 321.879
EBAY 30.06.2019 $0.46 321.356
EBAY 31.03.2019 $0.57 320.833
Thanks in advance for any help!
2
Answers
There is a $ sign in Sales. Maybe Sales was converted to a character vecter during data import? You can remove the sign and convert it to numeric. Here are two possible variations of
my_correlation()
– one usessubset()
and another[
.Data:
tidiverse