Reputation: 598
I have a dataframe similar to the below:
v1 v2 v3 v4 v5
a a1 a2 a3 a4
b b1 b2
c c1 c2 c3
I want to convert this to lists as below.
lista <- list(base="a", alts=c("a1","a2","a3","a4"))<br>
listb <- list(base="b", alts=c("b1","b2"))<br>
listb <- list(base="c", alts=c("c1","c2","c3"))
I have looked at solutions posted on here and tried some suggestions, but nothing works?1 Any help will be great! I am still new to R - Cheers
Upvotes: 1
Views: 101
Reputation: 54237
If df
contains your data frame, you could try like this:
l <- lapply(as.data.frame(t(df), stringsAsFactors = FALSE),
function(x) {
x <- unname(x)
list(base = x[1], alts = x[-1])
})
names(l) <- paste0("list", df[, 1])
list2env(l, envir = .GlobalEnv)
lista
# $base
# [1] "a"
#
# $alts
# [1] "a1" "a2" "a3" "a4"
Upvotes: 4