Correlation of specific numerical variables only

Hi R Masters,
I have this simple data frame:

source <- data.frame(
  stringsAsFactors = FALSE,
                                    URN = c("aaa","bbb","ccc",
                                            "ddd","eee","fff","ggg","hhh","iii"),
                                    QF8 = c("sdfsdf","gscfgb",NA,
                                            "fgjhfgj","gh","hgdfsg",NA,NA,
                                            "ffh"),
                                   QF16 = c("Yes","No","No","Yes",
                                            "Yes","Yes","Yes","No","Yes"),
                              QF70_Top2 = c(10,10,10,20,50,
                                            100,100,0,100),
                               Q12_Top2 = c(NA,100,20,50,NA,10,
                                            NA,50,NA),
                               QF2_Top2 = c(100,10,100,50,100,
                                            100,20,0,10),
                                    QF4 = c(100,10,100,100,100,
                                            100,10,0,100)
                     )



source

Now I have to find correlations between all variables containing "Top2".
I can select them manually and do this:

library(Hmisc)
correlation <-rcorr(cbind(source$QF70_Top2, source$Q12_Top2, source$QF2_Top2), type="pearson")

corr_value <-round(correlation[["r"]], 3)
corr_value
p_value <-round(correlation[["P"]], 3)
p_value
Count <-round(correlation[["n"]], 3)
Count

require(openxlsx)
corr.tables <- list("Correlation" = corr_value, "P-Value" = p_value,"Count" = Count)
write.xlsx(corr.tables, file = "C:/Users/.../Correlation tables.xlsx")

But I believe I could use something like this:

(vars(matches("Top2"))

...in my code.
Unfortunatelly, I don't know how to do that.
Also, is it possible to add variable names in my outputs? I can see only V1, V2 and V3 instead of QF70_Top2, Q12_Top2 and QF2_Top2

Can you help?

select(source,matches("Top2")) %>%
  as.matrix %>% 
  rcorr(type="pearson")
1 Like

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.