Is it possible to put list in a table or make a set in R?

Viewed 41

I am using R to align the relationships between almost 40,000 items. There are two types of relationships 0, -1. To give a simple example, the data is in the form like

a|b|0
a|c|1
a|d|0
b|a|0
b|c|0
b|d|1

and I wish to align them in the form of

a{
  0: b,d
  1: c
},
b{
  0: a,c
  1: d
}

to make it easier to search and see the relation between items. I'm not sure even it is possible, but I would really love a help from R experts.

1 Answers

Assuming your data is in a dataframe

df=read.table(text="
a|b|0
a|c|1
a|d|0
b|a|0
b|c|0
b|d|1",h=F,sep="|")

then

lapply(
  split(df,df$V1),
  function(x){
    split(x$V2,x$V3)
  }
)

$a
$a$`0`
[1] "b" "d"

$a$`1`
[1] "c"

$b
$b$`0`
[1] "a" "c"

$b$`1`
[1] "d"

it's not a dictionary but a list.

Related