Michael Schubert
Michael Schubert

Reputation: 2796

R: converting table to list of vectors

I've got an R data.frame TAB with the following content:

A A
A B
A C
B A
B D

What I would like to get is a list of vectors/lists:

A -> A, B, C
B -> A, D

I can do this with a for loop that looks something like this:

for (i in 1:2){
    V[[i]]<-TAB[which(is.element(TAB[,1],UA[i])),2]
}

Is there a more convenient way of doing this that avoids the for loop?

Upvotes: 1

Views: 2285

Answers (1)

Michael Schubert
Michael Schubert

Reputation: 2796

So in that case, the snippet to solve the problem is:

mysplit = split(dd$y, dd$x)
myresult = lapply(mysplit, as.character)

Upvotes: 1

Related Questions