Creating a named list from two vectors (names, values)

前端 未结 3 1056
谎友^
谎友^ 2020-12-24 04:54

Is there a way to use mapply on two vectors to construct a named list? The first vector would be of type character and contain the names used for the list while

3条回答
  •  醉酒成梦
    2020-12-24 05:41

    What I propose is made in 2 steps, and it's quite straightforward, so maybe it's easier to understand:

    test_list <- list(1, 2)
    names(test_list) <- c("foo", "bar")
    

    What @ben-bolker proposes works, but just wanted to share an alternative, in case you prefer it.

    Happy coding!

提交回复
热议问题