首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将两个具有不同行/Col名称的数据名组合在一起

将两个具有不同行/Col名称的数据名组合在一起
EN

Stack Overflow用户
提问于 2021-10-08 06:01:20
回答 1查看 54关注 0票数 1

我有一个数据框架:

代码语言:javascript
运行
复制
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"))

用这个,我创建了一个矩阵

代码语言:javascript
运行
复制
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)根据类别列获取事件的所有计数。就这样,我做了:

代码语言:javascript
运行
复制
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")

尝试将两个独立的矩阵组合在一起。

代码语言:javascript
运行
复制
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。

我的预期结果应该是这样:

代码语言:javascript
运行
复制
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做了另一半,但是我很难把它们合并到我想要的结果中去。

编辑

代码语言:javascript
运行
复制
newTest <- test[,-1]
rownames(newTest) <- test[,1]
newTry <- merge(anotherMatrix, newTest, by = "row.names")

作为一个额外的尝试方法-这也接近我的预期输出,但不包括对手行,也加倍每列。

代码语言:javascript
运行
复制
dfList <- c("P1", "P2", "P3", "P4", "P5")

编辑2

快速跟踪两个不同行长的DF组合,我将如何将passesComb + copyComb组合成gamesComb

代码语言:javascript
运行
复制
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")

代码语言:javascript
运行
复制
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")

copyCombpassesComb相同,只是删除了行/列3。我试着从代码中修改原来的答案。

代码语言:javascript
运行
复制
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,并且除了不添加单元格值之外,也不会添加行/列名。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-08 06:27:12

如果目的是用goodMatrix更新“goodMatrix”,请使用row.namescolnames从“goodMatrix”到“anotherMatrix”子集,并将“goodMatrix”分配给“anotherMatrix”

代码语言:javascript
运行
复制
anotherMatrix[row.names(goodMatrix), colnames(goodMatrix)] <- goodMatrix

然后,我们将NA替换为0

代码语言:javascript
运行
复制
anotherMatrix[is.na(anotherMatrix)] <- 0

-checking与“expectedOutput”

代码语言:javascript
运行
复制
> identical(expectedOutput, anotherMatrix[names(expectedOutput)])
[1] TRUE
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69491160

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档