Ich möchte einen gerichteten Graphen in R machen, indem ich 2 Datenrahmen mache: einen für Scheitelpunkte und einen für Kanten. Auch sollte mein Graph diese Attribute haben:Abtastung eines Graphen in R
- Kein Kreis (also keine A -> A)
- Es ist maximal 1 Kante zwischen zwei Knoten.
komme ich mit dem Code wie unten:
library(dplyr)
USER_BASE <- 1:100
MAXIMUM_USER_RELATIONSHIP = length(USER_BASE)*4
my_random <- function(vector, size, replace, seed)
{
set.seed(seed)
return(sample(vector, size = size, replace = replace))
}
user <- data.frame(
id = USER_BASE
)
relationship <- data.frame(
from = my_random(USER_BASE, MAXIMUM_USER_RELATIONSHIP, replace = TRUE, 1),
to = my_random(USER_BASE, MAXIMUM_USER_RELATIONSHIP, replace = TRUE, 2)
) %>% filter(from != to) %>% unique()
Mein Code ermöglicht noch zwei Kanten zwischen zwei Knoten (A -> B und B -> A). Wie könnte ich das nur 1 Kante zwischen A und B erreichen?
Mit freundlichen Grüßen
Edit: Unique rows, considering two columns, in R, without order
Meinen vollständigen Code:: Ich habe eine Lösung aus gefunden
library(dplyr)
library(data.table)
USER_BASE <- 1:100
MAXIMUM_USER_RELATIONSHIP = length(USER_BASE)*4
my_random <- function(vector, size, replace, seed)
{
set.seed(seed)
return(sample(vector, size = size, replace = replace))
}
user <- data.frame(
id = USER_BASE
)
relationship <- data.frame(
from = my_random(USER_BASE, MAXIMUM_USER_RELATIONSHIP, replace = TRUE, 1),
to = my_random(USER_BASE, MAXIMUM_USER_RELATIONSHIP, replace = TRUE, 2)
) %>% filter(from != to) %>% unique()
relationship <- unique(as.data.table(relationship)[, c("from", "to") := list(pmin(from, to),
pmax(from, to))], by = c("from", "to"))
Bitte geben Sie eine [MCVE] –
fand ich hier eine Lösung: http://stackoverflow.com/questions/28574006/unique-combination-of-two-columns-in-r –
@ StevenBeaupré: Ich habe bereits hat den Code gepostet. –