I am trying to run several correlations in R by adapting code I have used to do this for regressions.
This is the existing code that I have used successfully for regressions.
combineddvs <- colnames(dfc[,87:100])
form <- paste("yourbehavior_c ~ days + days2 +", combineddvs)
models <- form %>%
set_names(combineddvs) %>%
map(~lm(.x, data = dfc))
map(models, summary)
This is my adaptation of it for the correlations I'd like to run.
combineddvs <- c("committed", "goodfaith", "strongfeel")
form <- paste("df$main, df$", combineddvs)
models <- form %>%
set_names(combineddvs) %>%
map(~cor(.x))
map(models, summary)
The first two lines work, but I get this error when trying to create the object "models":
Error in cor(.x) : supply both 'x' and 'y' or a matrix-like 'x'
I know that I need both an x and a y to run a correlation. However, I am trying to include both of these in the "form" object.
This is a clunky way of showing what I am trying to do. I have more than 3 items, so this is not efficient in practice.
cor(df$main, df$committed)
cor(df$main, df$goodfaith)
cor(df$main, df$strongfeel)
How can I do this? I am open to modifying the above code or to using an entirely different approach.