Make correlations between lists

I'm trying to check the relationship of two weather variables in list format, because I'm putting data from the last six months (for example) in a list and so on for every month of the year. But I would like to know if there is a way to apply correlation to these lists. I know that the data used in the cor function must be numeric, but in the case of list, how to proceed? Does R have a specific role that performs this work?

For example, in python I found posts that indicated a way, it would be something like this this and this

Part of the two lists I would like to correlate with variable V1 and V2

V1                                                                                                                                                             
c(149.6, 251, 161, 244.3, 760.7, 185.5)
c(26.434432, 26.192469, 26.379851, 26.158357, 25.848118, 26.676528)
c(251, 161, 244.3, 760.7, 185.5, 44.3)
c(26.192469, 26.379851, 26.158357, 25.848118, 26.676528, 26.655914)
c(161, 244.3, 760.7, 185.5, 44.3, 3.8)

V2
c(26.379851, 26.158357, 25.848118, 26.676528, 26.655914, 26.85959)
c(244.3, 760.7, 185.5, 44.3, 3.8, 0.5)
c(26.158357, 25.848118, 26.676528, 26.655914, 26.85959, 27.271498)
c(760.7, 185.5, 44.3, 3.8, 0.5, 2.2)
c(25.848118, 26.676528, 26.655914, 26.85959, 27.271498, 0)

I appreciate any help!

Is this what you are trying to do?

library(purrr)
V1 <- list(                                                                                                     
c(149.6, 251, 161, 244.3, 760.7, 185.5),
c(26.434432, 26.192469, 26.379851, 26.158357, 25.848118, 26.676528),
c(251, 161, 244.3, 760.7, 185.5, 44.3),
c(26.192469, 26.379851, 26.158357, 25.848118, 26.676528, 26.655914),
c(161, 244.3, 760.7, 185.5, 44.3, 3.8))

V2 <- list(
c(26.379851, 26.158357, 25.848118, 26.676528, 26.655914, 26.85959),
c(244.3, 760.7, 185.5, 44.3, 3.8, 0.5),
c(26.158357, 25.848118, 26.676528, 26.655914, 26.85959, 27.271498),
c(760.7, 185.5, 44.3, 3.8, 0.5, 2.2),
c(25.848118, 26.676528, 26.655914, 26.85959, 27.271498, 0))
Cors <- map2(.x = V1, .y = V2, .f = ~cor(.x, .y))
Cors
#> [[1]]
#> [1] 0.3245963
#> 
#> [[2]]
#> [1] -0.04686047
#> 
#> [[3]]
#> [1] -0.09022814
#> 
#> [[4]]
#> [1] -0.1934669
#> 
#> [[5]]
#> [1] 0.4059379

#Confirm  two cases manually
cor(V1[[1]], V2[[1]])
#> [1] 0.3245963
cor(V1[[5]], V2[[5]])
#> [1] 0.4059379

Created on 2021-11-30 by the reprex package (v2.0.1)

1 Like

Thank you very much for the reply and it works very well. I noticed that it makes the correlation list by list and I was left with a question, if you can answer I would be grateful. If I only want to work a direct result of all the correlations in the lists, would you know how to proceed? I thought about transforming it into a dataframe, but I'm afraid that thought is not correct.

I am not sure what you mean by

to work a direct result of all the correlations in the lists

Do you want a numeric vector of the results instead of a list? You can get that with the map2_dbl() function.

Cors <- map2_dbl(.x = V1, .y = V2, .f = ~cor(.x, .y))
Cors
[1]  0.32459635 -0.04686047 -0.09022814 -0.19346693  0.40593787
1 Like

Thank you so much for the feedback, that was exactly it!

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.