0

I have an edge list with names of organizations and their connections to other organizations. I am wondering whether there is an efficient way in R to create a table in which one column lists the outgoing nodes, and the other column contains a single string with all incoming nodes.

So basically from:

Column A Column B
A C
A D
B C

To

Column A Column B
A C, D
B C

I tried to various things with Melt and Cast but couldn't make it to work properly with strings.

1 Answers1

0

Based on your desired output we could Group column A using group_by function from dplyr and make comma-separated values in Column B:

library(tidyverse)
# your sample data
df <- data.frame(
  Column_A = c("A", "A", "B"),
  Column_B = c("C", "D", "C")
)

# Grouping column A using group_by function from dplyr and make comma-separated values in Column B
df <- df %>%
  group_by(Column_A) %>%
  summarize(Column_B = paste(Column_B, collapse = ", "))

# Renaming your column names
names(df) <- c("Column A", "Column B")

df<-as.data.frame(df)
df
  Column A Column B
1        A     C, D
2        B        C
S-SHAAF
  • 1,863
  • 2
  • 5
  • 14