作者:D大龙 | 来源:互联网 | 2023-08-20 10:22
set.seed(8)df<-data.frame(Asample(c(1:3),10,replaceT),Bsample(c(1:3),10,replace
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:
希望将列的子集传递到dplyr mutate()中,并进行逐行计算,例如cor(),以获得列a - c和D-F之间的相关性,但无法知道如何进行。在这里、这里和这里找到如此多的灵感,但仍然无法生成可接受的代码。例如,我尝试过:
require(plyr)
require(dplyr)
df %>%
rowwise() %>%
mutate(c=cor(.[[1:3]],.[[4:6]]))
1 个解决方案