0

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.

melbez
  • 960
  • 1
  • 13
  • 36
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Mar 28 '20 at 20:24

2 Answers2

2

With data.table, you could use .SD together with lapply. For instance, with mtcars data:

library(data.table)
dt <- data.table(mtcars)

dt[, .(lapply(.SD, cor, mpg)), .SDcols = colnames(dt)[2:length(colnames(dt))]]

So, in your example,

dt[, .(lapply(.SD, cor, main)), .SDcols = c("committed","goodfaith","strongfeel")]]
linog
  • 5,786
  • 3
  • 14
  • 28
  • This doesn't seem to be working for me. What does the . before the lapply function mean and before SD? – melbez Mar 29 '20 at 00:53
  • Did you convert your object to be `data.table` (for instance `dt <- as.data.table(df)`) ? `.` is a shortcut for `list` in `data.table`. The syntax `dt[,.(),by = ...]` is the `data.table` way to do `df %>% group_by(...) %>% summarise(.)` – linog Mar 29 '20 at 08:23
0

We can use dplyr

library(dplyr)
mtcars %>%
     summarise_at(-1, ~ cor(., mpg))
#     cyl       disp         hp      drat         wt     qsec        vs        am      gear       carb
#1 -0.852162 -0.8475514 -0.7761684 0.6811719 -0.8676594 0.418684 0.6640389 0.5998324 0.4802848 -0.5509251
akrun
  • 874,273
  • 37
  • 540
  • 662