Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R - two data frame columns to list of key-value pairs

Tags:

r

Say I have a data frame

DF1 <- data.frame("a" = c("a", "b", "c"), "b" = 1:3)

What is the easiest way to turn this into a list?

DF2 <- list("a" = 1, "b" = 2, "c" = 3)

It must be really simple but I can't find out the answer.

like image 608
paljenczy Avatar asked Jun 18 '15 21:06

paljenczy


1 Answers

You can use setNames and as.list

DF2 <- setNames(as.list(DF1$b), DF1$a)
like image 134
MrFlick Avatar answered Oct 25 '22 15:10

MrFlick