-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_nodes_from_links.R
43 lines (37 loc) · 1.49 KB
/
get_nodes_from_links.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#' Get Nodes From Links
#'
#' This function takes a data frame of links and creates the respective nodes dataframe.
#'
#' @param Links A data frame containing links information.
#' @param source The name of the column in the Links data frame that contains the source nodes. Default is "source".
#' @param target The name of the column in the Links data frame that contains the target nodes. Default is "target".
#'
#' @return A data frame of unique nodes.
#' @export
#'
#' @examples
#' links <- data.frame(source = c("A", "B", "C"), target = c("B", "C", "A"))
#' get_nodes_from_links(links)
get_nodes_from_links <- function(Links,
source = "source",
target = "target") {
# Check if source and target columns are present in Links
if (!all(c(source, target) %in% colnames(Links))) {
# Check if source column exists
if (!(source %in% colnames(Links))) {
sourcenotfound <- source
source <- colnames(Links)[1]
message(paste("The source column '", sourcenotfound, "' was not found. Using the first column: ", source))
}
# Check if target column exists
if (!(target %in% colnames(Links))) {
targetnotfound <- target
target <- colnames(Links)[2]
message(paste0("The target column '", targetnotfound, "' was not found. Using the second column: ", target))
}
}
nodes <- c(Links[[source]], Links[[target]])
nodes <- sort(unique(nodes))
NodesDF <- data.frame(NodeID = nodes)
return(NodesDF)
}