meitei
November 3, 2020, 4:03pm
1
Hi All,
How can I proceed to estimate the correlation of 4 variables( cor(a,b) and cor(c,d) ) for each month?
I prefer to use aggregate and I tried the code below. variables are not able to recognize x and y.
I would be glad if you can suggest it.
Thanks
date=seq(as.Date("2020/1/1"), as.Date("2020/5/1"), "days")
a=rnorm(length(date))
b=rnorm(length(date))
c=rnorm(length(date))
d=rnorm(length(date))
dat <- data.frame(date,a,b,c,d)
dat$month=format(as.Date(dat$date, format="%Y-%m-%d"),"%m")
head(dat)
aggregate( cbind(c(a,b),c(c,d))~ month, dat, cor)
I am expecting the following output
Month cor(a,b), cor(c,d)
1 aa bb
2 cc dd
3 ee ff
4 gg hh
5 ii jj
library(tidyverse)
group_by(dat,
,month) %>%
summarise(cor_ab=cor(a,b),
cor_cd=cor(c,d))
meitei
November 3, 2020, 4:11pm
3
Hi @nirgrahamuk , I'm not sure why I am not able to get for each month ? Can you please check again.
this is the output..
`summarise()` ungrouping output (override with `.groups` argument)
# A tibble: 5 x 3
month cor_ab cor_cd
<chr> <dbl> <dbl>
1 01 0.0627 -0.278
2 02 -0.267 0.0931
3 03 -0.0267 -0.116
4 04 -0.257 -0.0402
5 05 NA NA
1 Like
meitei
November 3, 2020, 4:28pm
5
@nirgrahamuk , my system shows only a single value while using such a code of summarise.
So, I tried with another code just to check my result why showing only one output.
Could you get explain why is this happening to me?
Thanks agian
Output:
library(tidyverse)
> group_by(dat,month) %>%
+ summarise(cor_ab=cor(a,b),
+ cor_cd=cor(c,d))
cor_ab cor_cd
1 0.0517008 -0.09864212
another code that showed one output.
> library(dplyr)
> mtcars %>%
+ group_by(am) %>%
+ summarise(mpg = sum(mpg))
mpg
1 642.9
>
perhaps your R session is in a bad state and you should restart it...(Ctrl+Shift+F10)
library(tidyverse)
mtcars %>%
group_by(am) %>%
summarise(mpg = sum(mpg))
`summarise()` ungrouping output (override with `.groups` argument)
# A tibble: 2 x 2
am mpg
<dbl> <dbl>
1 0 326.
2 1 317.
1 Like
meitei
November 3, 2020, 4:32pm
7
Yes, I got it after detachment of dplyr.
Thanks..!
system
Closed
November 10, 2020, 4:32pm
8
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed. If you have a query related to it or one of the replies, start a new topic and refer back with a link.