- I have a dataframe
events
with xy-coords of unique points. - I have a dataframe
all_nodes
with xy-coords of network nodes. All points ofevents
are also inall_nodes
, but not necessarily only once, and at different positions, i.e., the index (row id) of a point inevents
does not correspond toall_nodes
. - I have a matrix
ma
of dimensionnrow(all_nodes)
timesnrow(all_nodes)
with calculated pairwise interaction terms between all nodes.ma
rows and cols correspond with the index (row_ids) ofall_nodes
.
My overall goal is to identify the row ids of events
in all_nodes
. With this I am aiming to extract a submatrix of pairwise interaction from my matrix ma
according to the detected row ids. Finally I want to change the order of the submtarix such that the ids and correponding points correspond to events
. Any kind of help (code/reference/hint) is much appreciated!
Toy data (you can find real data below)
# coords of unique events
events <- data.frame(x = c(1,2,3,4),
y = c(4,3,2,1))
# all_nodes
all_nodes <- data.frame(x = c(2,1,120,3,150,4,1),
y = c(3,4,120,2,150,1,4))
# matrix corresponding to the index of all_nodes
ma <- matrix(data = rnorm(n = 49, mean = 3, sd = 1),
nrow = nrow(all_nodes), ncol = nrow(all_nodes))
ma[6, ] <- ma[2, ]
My effort which isn't quite helpful, since I ran in several problems.
# coords of unique events
events # see toy data
# ------------------------------------------------
# from object g of class "sfnetwork" "tbl_graph" "igraph"
# all rounded coords of nodes; from g ma is used
# in several steps
# cols and rows in ma correspond to node ids of g/all_nodes
# all_nodes <- g %>% tidygraph::activate("nodes") %>%
# as.data.frame(geometry)
# all_nodes <- as.data.frame(matrix(unlist(all_nodes$geometry), ncol = 2, byrow = TRUE))
# names(all_nodes) <- c('x', 'y')
# all_nodes <- round(all_nodes, 2)
# --------------------------------------------------
# matching based on x-coord only
ix <- which(all_nodes$x %in% events$x)
# Problem A
length(ix) == nrow(events) # different length
# Problem B
# and the event with coords x=1, y=4 occurs twice in ix
sub <- ma[ix, ix]
# If problems A+B were eleminated, sub would correspond to
# all events, but I different indexing makes it unusable #(several permutations possible)
I also played around with st_equals {sf}
to compare geometries directlly using events <- sf::st_as_sf(events[, c('x', 'y')], coords = c('x', 'y'))
in a previous step.
Real data
# removed