I have a data frame in R (taken from the dplyr's site here):
library(dplyr)
gdf <-
tibble(g = c(1, 1, 2, 3), v1 = 10:13, v2 = 20:23) %>%
group_by(g)
gdf
Resulting to:
# A tibble: 4 × 3
# Groups: g [3]
g v1 v2
<dbl> <int> <int>
1 1 10 20
2 1 11 21
3 2 12 22
4 3 13 23
Now I have a vector :
y <- rnorm(4);y
I want to measure the correlation of y with v1 and the correlation of y with v2 simultaneously.
The across()
function might do the job
gdf %>% mutate(across(v1:v2, ~ cor(.x,y)))
but R reports me an error :
Error: Problem with `mutate()` input `..1`.
ℹ `..1 = across(v1:v2, ~cor(.x, y))`.
x incompatible dimensions
ℹ The error occurred in group 1: g = 1.
Run `rlang::last_error()` to see where the error occurred.