这是我打算做的(对于相当多的变量和数据集):
mygroupdf <- data.frame (varname = c("A", "B", "c1", "D2",
"E", "F", "g1"), group = c(1, 1, 1, 2,3,3,4))
> mygroupdf
varname group
1 A 1
2 B 1
3 c1 1
4 D2 2
5 E 3
6 F 3
7 g1 4
此数据框仅包含用于变量分组的信息:
group 1 = A, B, c1
group 2 = D2
group 3 = E, F
group 4 = g1
第二个数据集 - 包含实际数据
set.seed(1234)
dataf <- data.frame (yvar = rnorm (10, 10,3),
A = sample(c(1,0), 10, T), B = sample(c(1,0), 10, T),
c1 = sample (c(1,0), 10, T), D2 = sample (c(1,0), 10, T),
E= sample (c(1,0), 10, T),F = sample (c(1,0), T),
g1 = sample (c(1,0), 10, T))
# manual workout:
xtemp <- dataf$A* dataf$B * dataf$c1 # all from group 1
# I error in previous version it is * not +
# (is product of all members of a group i.e.
xtemp <- dataf$D2 (- group 2)
xtemp <- dataf$E * dataf$F (- group 3)
xtemp <- dataf$G (- group 4)
然后将产品与 Yvar 相关:
x <- cor(dataf$yvar, xtemp)
我想将它包装到一个函数中,以便我可以将它应用到我的数据集中的 1000 组变量中。
corrfun <- function (x, V1, V2, V3) {
xtemp <- V1 * V2 + V3
x <- cor(dataf$yvar, xtemp)
return (x)
}
由于不同的组有不同的变量,我不确定如何构建这样的函数并应用于整个数据集。请帮忙 !
编辑:过程: