我有一组包含五列的篮球阵容数据,每列共享相同的因子,如下所示:
head(dat)
V1 V2 V3 V4 V5
1 MILES,KEATON KINGSLEY,MOSES BELL,ANTHLON HANNAHS,DUSTY DURHAM,JABRIL
2 MILES,KEATON KINGSLEY,MOSES BELL,ANTHLON HANNAHS,DUSTY DURHAM,JABRIL
3 KINGSLEY,MOSES BELL,ANTHLON HANNAHS,DUSTY DURHAM,JABRIL THOMPSON,TREY
4 KINGSLEY,MOSES BELL,ANTHLON HANNAHS,DUSTY THOMPSON,TREY BEARD,ANTON
5 THOMPSON,TREY BEARD,ANTON KOUASSI,WILLY WHITT,JIMMY WATKINS,MANUALE
6 THOMPSON,TREY BEARD,ANTON KOUASSI,WILLY WHITT,JIMMY WATKINS,MANUALE
我想要做的是让每一行都是行上显示的当前因子的虚拟编码,如下所示:
MILES,KEATON KINGSLEY,MOSES BELL,ANTHLON HANNAHS,DUSTY DURHAM,JABRIL THOMPSON,TREY BEARD,ANTON KOUASSI,WILLY WHITT,JIMMY WATKINS,MANUALE
1 1 1 1 1 0 0 0 0 0
1 1 1 1 1 0 0 0 0 0
0 1 1 1 1 1 0 0 0 0
但是,model.matrix 似乎只有一列的范围;它不会让我在多个列中共享整个因子集。遵循 [this thread][1] 中的一些建议,我尝试了:
df <- as.data.frame(lapply(dat,as.factor))
fList <- lapply(names(df),reformulate,intercept=FALSE)
mList <- lapply(fList,sparse.model.matrix,data=df)
br <- do.call(cBind,mList)
head(br)
6 x 31 sparse Matrix of class "dgCMatrix"
[[ suppressing 31 column names ‘V1BEARD,ANTON’, ‘V1BELL,ANTHLON’, ‘V1KINGSLEY,MOSES’ ... ]]
1 . . . 1 . . . . 1 . . 1 . . . . . . 1 . . . . . . 1 . . . . .
2 . . . 1 . . . . 1 . . 1 . . . . . . 1 . . . . . . 1 . . . . .
3 . . 1 . . . 1 . . . . . . 1 . . . 1 . . . . . . . . . . . 1 .
4 . . 1 . . . 1 . . . . . . 1 . . . . . . . 1 . . 1 . . . . . .
5 . . . . 1 1 . . . . . . . . 1 . . . . . . . . 1 . . . . . . 1
6 . . . . 1 1 . . . . . . . . 1 . . . . . . . . 1 . . . . . . 1
它结合了列名和因子名。我该怎么办?