0

I need to create a correlation matrix for my dataframe that contains both numerical and logical (binary) variables. Is it possible to do it in R? Do you have any suggestions?

Example:

a <- (data.frame(a=c(34,54,55,12,13,6), b=factor(c(FALSE,TRUE,TRUE,TRUE,TRUE,FALSE), 0:1), C=c(1:6)))

Thanks

Ludovico
  • 37
  • 5

1 Answers1

0

I am not sure whether your question is about the creation of a data.frame object with several types of variables (see comment) or how to compute correlations if your data is, as you mentioned, "numerical and binary" (*). This link might help. I assume in particular Dan Chaltiel's answer (last answer) will help you.

(*) In the letter case the thread is possibly a duplicate.


EDIT: Considering Dan Chaltiel's approach (see link), does this help?

df <- data.frame(a=c(34,54,55,12,13,6), 
                 b=c("FALSE","TRUE","TRUE","TRUE","TRUE","FALSE"), 
                 c=c(1:6))

library(dplyr)

model.matrix(~0+., data=df) %>% 
  cor(use="pairwise.complete.obs") 
Pax
  • 664
  • 4
  • 23
  • Yes, i refer to the second case you mentioned. Sorry for the lack of accuracy in asking, i'm a newbie :) – Ludovico Sep 07 '21 at 12:53
  • Does the link help? – Pax Sep 07 '21 at 13:01
  • I tried with the cor command, but it gives me the error "x must be numeric" – Ludovico Sep 07 '21 at 13:06
  • Can you please edit your question and provide data and code (see [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example))? – Pax Sep 07 '21 at 13:10
  • I edited my post, I just made use of things the link provides. – Pax Sep 07 '21 at 14:24
  • It worked! Thanks a lot... Though I didn't understand why. I clearly do not master the subject, i will try to read better the link you provided! THANKS :) – Ludovico Sep 07 '21 at 14:29