我有一个数据框架:
dtMatrix <- structure(list(category = c("Opponent", "Opponent", "Opponent",
"Opponent", "P1", "P2", "P3", "P4", "P2", "Opponent", "Opponent",
"P1"), Event = c("Good Pass", "Good Pass", "Good Pass", "Turnover",
"Good Pass", "Good Pass", "Good Pass", "Good Pass", "Good Pass",
"Intercepted Pass", "Bad Pass", "Good Pass"), Receiver = c(NA,
NA, NA, NA, "P2", "P3", "P4", "P5", "P1", NA, NA, "P2")), row.names = c(NA,
-12L), class = c("tbl_df", "tbl", "data.frame"))用这个,我创建了一个矩阵
goodMatrix <- dtMatrix %>%
filter(Event == 'Good Pass' & !is.na(Receiver)) %>%
dplyr::count(category, Receiver) %>%
tidyr::complete(category = dfList, Receiver = dfList, fill = list(n = 0)) %>%
pivot_wider(names_from = Receiver, values_from = n) %>%
column_to_rownames('category')此goodMatrix存储P1-P5之间良好传球的组合。在dtMatrix中,它还具有Event列中的其他值,例如周转率/截获关口,并对对手进行了说明。我想创建一个类似于goodMatrix的矩阵,但是对于前面提到的事件和对手。
countTypes <- dtMatrix %>% dplyr::count(category, Event)根据类别列获取事件的所有计数。就这样,我做了:
secondMatrix <- data.frame(matrix(ncol = length(unique(countTypes$Event)), nrow = length(unique(countTypes$category))))
rownames(secondMatrix) <- unique(countTypes$category)
colnames(secondMatrix) <- unique(countTypes$Event)
secondMatrix
test <- merge(goodMatrix, secondMatrix, by = "row.names")尝试将两个独立的矩阵组合在一起。
anotherMatrix <- dtMatrix %>%
dplyr::count(category, Event) %>%
tidyr::complete(category = dfList, Event = dfList, fill = list(n = 0)) %>%
pivot_wider(names_from = Event, values_from = n) %>%
column_to_rownames('category')这也会将它们添加到一个值中,但不会保留dtMatrix中的值,而是将其重置为0。
我的预期结果应该是这样:
expectedOutput <- structure(list(P1 = c(0, 1, 0, 0, 0, 0), P2 = c(2, 0, 0, 0, 0,
0), P3 = c(0, 1, 0, 0, 0, 0), P4 = c(0, 0, 1, 0, 0, 0), P5 = c(0,
0, 0, 1, 0, 0), `Good Pass` = c(2, 2, 1, 1, 0, 3), `Bad Pass` = c(0,
0, 0, 0, 0, 1), `Intercepted Pass` = c(0, 0, 0, 0, 0, 1), Turnover = c(0,
0, 0, 0, 0, 1)), row.names = c("P1", "P2", "P3", "P4", "P5",
"Opponent"), class = "data.frame")anotherMatrix做了一半,而dtMatrix做了另一半,但是我很难把它们合并到我想要的结果中去。
编辑
newTest <- test[,-1]
rownames(newTest) <- test[,1]
newTry <- merge(anotherMatrix, newTest, by = "row.names")作为一个额外的尝试方法-这也接近我的预期输出,但不包括对手行,也加倍每列。
dfList <- c("P1", "P2", "P3", "P4", "P5")编辑2
快速跟踪两个不同行长的DF组合,我将如何将passesComb + copyComb组合成gamesComb
passesComb <- structure(list(P1_Good = c(0, 1, 0, 0, 0, 0, 1), P2_Good = c(2,
0, 0, 0, 0, 0, 2), P3_Good = c(0, 1, 0, 0, 0, 0, 1), P4_Good = c(0,
0, 1, 0, 0, 0, 1), P5_Good = c(0, 0, 0, 1, 0, 0, 1), P1_Bad = c(0,
0, 0, 0, 0, 0, 0), P2_Bad = c(0, 0, 0, 0, 0, 0, 0), P3_Bad = c(0,
0, 0, 0, 0, 0, 0), P4_Bad = c(0, 0, 1, 0, 0, 0, 1), P5_Bad = c(0,
0, 0, 0, 0, 0, 0), `Bad Pass` = c(0, 0, 1, 0, 0, 1, 1), `Good Pass` = c(2,
2, 1, 1, 0, 3, 6), `Intercepted Pass` = c(0, 0, 0, 0, 0, 1, 0
), Turnover = c(0, 0, 0, 0, 0, 1, 0), totalEvents = c(2, 2, 2,
1, 0, 6, 7)), row.names = c("P1", "P2", "P3", "P4", "P5", "Opponent",
"VT"), class = "data.frame")和
copyComb <- structure(list(P1_Good = c(0, 1, 0, 0, 0, 1), P2_Good = c(2,
0, 0, 0, 0, 2), P4_Good = c(0, 0, 0, 0, 0, 1), P5_Good = c(0,
0, 1, 0, 0, 1), P1_Bad = c(0, 0, 0, 0, 0, 0), P2_Bad = c(0, 0,
0, 0, 0, 0), P3_Bad = c(0, 0, 0, 0, 0, 0), P4_Bad = c(0, 0, 0,
0, 0, 1), P5_Bad = c(0, 0, 0, 0, 0, 0), `Bad Pass` = c(0, 0,
0, 0, 1, 1), `Good Pass` = c(2, 2, 1, 0, 3, 6), `Intercepted Pass` = c(0,
0, 0, 0, 1, 0), Turnover = c(0, 0, 0, 0, 1, 0), totalEvents = c(2,
2, 1, 0, 6, 7)), row.names = c("P1", "P2", "P4", "P5", "Opponent",
"VT"), class = "data.frame")copyComb与passesComb相同,只是删除了行/列3。我试着从代码中修改原来的答案。
gamesComb <- data.frame(matrix(NA, nrow = ifelse(nrow(passesComb) >= nrow(copyComb), nrow(passesComb),nrow(copyComb)),
ncol = ifelse(ncol(passesComb) >= ncol(copyComb), ncol(passesComb),ncol(copyComb))))
gamesComb[row.names(ifelse(nrow(passesComb) >= nrow(copyComb), passesComb, copyComb)),
colnames(ifelse(ncol(passesComb) >= ncol(copyComb), passesComb, copyComb))] <- passesComb但这只会创建一个7x15df,并且除了不添加单元格值之外,也不会添加行/列名。
发布于 2021-10-08 06:27:12
如果目的是用goodMatrix更新“goodMatrix”,请使用row.names和colnames从“goodMatrix”到“anotherMatrix”子集,并将“goodMatrix”分配给“anotherMatrix”
anotherMatrix[row.names(goodMatrix), colnames(goodMatrix)] <- goodMatrix然后,我们将NA替换为0
anotherMatrix[is.na(anotherMatrix)] <- 0-checking与“expectedOutput”
> identical(expectedOutput, anotherMatrix[names(expectedOutput)])
[1] TRUEhttps://stackoverflow.com/questions/69491160
复制相似问题