set.seed(8)
df <- data.frame(
A=sample(c(1:3), 10, replace=T),
B=sample(c(1:3), 10, replace=T),
C=sample(c(1:3), 10, replace=T),
D=sample(c(1:3), 10, replace=T),
E=sample(c(1:3), 10, replace=T),
F=sample(c(1:3), 10, replace=T))
Would like to pass a subset of columns into a dplyr mutate()
and make a row-wise calculation, for instance cor()
to get correlation between column A-C and D-F, but cannot figure out how. Found SO inspiration here, here and here, but nevertheless failed to produce an acceptable code. For instance, I tried this:
require(plyr)
require(dplyr)
df %>%
rowwise() %>%
mutate(c=cor(.[[1:3]],.[[4:6]]))
do
in order to runcor
. – Prim