Skip to content

Instantly share code, notes, and snippets.

@dfeng
Created July 20, 2015 14:19
Show Gist options
  • Select an option

  • Save dfeng/c818d181fcd4051c9880 to your computer and use it in GitHub Desktop.

Select an option

Save dfeng/c818d181fcd4051c9880 to your computer and use it in GitHub Desktop.
Pipes
# Before
# directed case
graph.directed[[i]] <- graph.adjacency(x, mode="directed", weighted=TRUE, diag=FALSE)
E(graph.directed[[i]])$type <- ifelse(E(graph.directed[[i]])$weight == 1, "friend", "enemy")
graph.sib <- graph.adjacency(aj.sib, mode="directed", weighted=TRUE, diag=FALSE)
E(graph.sib)$type <- "sibling"
graph.directed[[i]] <- graph.directed[[i]] + graph.sib
E(graph.directed[[i]])$type <- ifelse(is.na(E(graph.directed[[i]])$type_1), "sibling", E(graph.directed[[i]])$type_1)
E(graph.directed[[i]])$weight <- ifelse(is.na(E(graph.directed[[i]])$weight_1), 1, E(graph.directed[[i]])$weight_1)
graph.directed[[i]] <- remove.edge.attribute(graph.directed[[i]], "type_1")
graph.directed[[i]] <- remove.edge.attribute(graph.directed[[i]], "type_2")
graph.directed[[i]] <- remove.edge.attribute(graph.directed[[i]], "weight_1")
graph.directed[[i]] <- remove.edge.attribute(graph.directed[[i]], "weight_2")
E(graph.directed[[i]])$color <- ifelse(E(graph.directed[[i]])$weight == 1, ifelse(E(graph.directed[[i]])$type == "sibling", "dodgerblue3", "forestgreen"), "indianred3")
# After
graph.directed[[i]] <- graph_from_adjacency_matrix(
x,
mode="directed",
weighted=TRUE,
diag=FALSE
) %>%
set_edge_attr("type",
value=ifelse(edge_attr(., "weight") == 1, "friend", "enemy")
) %>%
union(graph.sib) %>%
set_edge_attr("type",
value=ifelse(is.na(edge_attr(., "type_1")), "sibling", edge_attr(., "type_1"))
) %>%
set_edge_attr("weight",
value=ifelse(is.na(edge_attr(., "weight_1")), 1, edge_attr(., "weight_1"))
) %>%
delete_edge_attr("type_1") %>%
delete_edge_attr("type_2") %>%
delete_edge_attr("weight_1") %>%
delete_edge_attr("weight_2") %>%
set_edge_attr("color",
value=ifelse(edge_attr(., "weight") == 1, ifelse(edge_attr(., "type") == "sibling", "dodgerblue3", "forestgreen"), "indianred3")
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment