gpt4 book ai didi

Make a adjacency matrix from dataframe when rows are equal(当行相等时,从数据帧生成邻接矩阵)

转载 作者:bug小助手 更新时间:2023-10-25 22:05:54 24 4
gpt4 key购买 nike



I have a dataframe similar to

我有一个类似于


mydf <- data.frame(Country=c('USA','Brazil','China','Italy','Ghana','Brazil','USA','China','USA'),
Pattern=c('XXZ','XXX','XYX','XXZ','XXX','XXX','XYZ','XXX','XYZ'),
Value=c(1,2,5,4,1,2,3,1,6))

I need an undirected adjacency matrix if the rows of Pattern are equal, which adding values.

我需要一个无向邻接矩阵,如果模式的行是相等的,这增加了价值。


For example,

例如,


    From     To   Pattern  Value
Brazil Brazil XXX 4
Brazil China XXX 3
Brazil Ghana XXX 3

更多回答

you are right, corrected

你是对的,更正

why isn't China Ghana a row in the matrix?

为什么中国加纳不是矩阵中的一行?

@Mark, my list is just an example, not a complete one. China and Ghana is one of the possible combination should be in the row.

@Mark,我的清单只是一个例子,不是完整的。中国和加纳队的可能组合之一应该是在排位上。

优秀答案推荐

It sounds like you're wanting this:

听起来你想要这个:


library(tidyverse)

mydf %>%
split(.$Pattern) %>%
map_dfr(~ .x %>%
mutate(n = row_number()) %>%
cross_join(., .) %>%
filter(n.x != n.y, Country.x <= Country.y) %>%
reframe(Value = `Value.x` + Value.y, .by = c(Country.x, Country.y)) %>%
rename(From = Country.x, To = Country.y) %>%
distinct(), .id = 'Pattern')

Output:

产出:


  Pattern   From     To Value
1 XXX Brazil Ghana 3
2 XXX Brazil Brazil 4
3 XXX Brazil China 3
4 XXX China Ghana 2
5 XXZ Italy USA 5
6 XYZ USA USA 9


Using combn in by with case handling.

在案件处理中使用Comn in By。


by(d, d$Pattern, \(x) {
u <- x$Country
out <- if (length(u) > 1) {
combn(u, 2, FUN=\(z) {
s <- x[x$Country %in% z, ]
if (length(table(z)) > 1) {
s <- unique(s)
}
with(s, data.frame(Country[1], Country[2], Pattern[1], sum(Value)))
}, simplify=FALSE) |> do.call(what='rbind') |> unique()
} else {
with(x, data.frame(Country, NA, Pattern, Value))
# with(x, data.frame(Country, NA, Pattern, NA)) ## alternatively
}
setNames(out, c('From', 'To', 'Pattern', 'Value'))
}) |> c(make.row.names=FALSE) |> do.call(what='rbind')
# From To Pattern Value
# 1 Brazil Ghana XXX 3
# 2 Brazil Brazil XXX 4
# 3 Brazil China XXX 3
# 4 Ghana China XXX 2
# 5 USA Italy XXZ 5
# 6 China <NA> XYX 5
# 7 USA USA XYZ 9



Data:

数据:


d <- structure(list(Country = c("USA", "Brazil", "China", "Italy", 
"Ghana", "Brazil", "USA", "China", "USA"), Pattern = c("XXZ",
"XXX", "XYX", "XXZ", "XXX", "XXX", "XYZ", "XXX", "XYZ"), Value = c(1,
2, 5, 4, 1, 2, 3, 1, 6)), class = "data.frame", row.names = c(NA,
-9L))

更多回答

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com