Convert named list to vector with values only

RList

R Problem Overview


I have a list of named values:

myList <- list('A' = 1, 'B' = 2, 'C' = 3)

I want a vector with the value 1:3

I can't figure out how to extract the values without defining a function. Is there a simpler way that I'm unaware of?

library(plyr)
myvector <- laply(myList, function(x) x)

Is there something akin to myList$Values to strip the names and return it as a vector?

R Solutions


Solution 1 - R

Use unlist with use.names = FALSE argument.

unlist(myList, use.names=FALSE)

Solution 2 - R

purrr::flatten_*() is also a good option. the flatten_* functions add thin sanity checks and ensure type safety.

myList <- list('A'=1, 'B'=2, 'C'=3)

purrr::flatten_dbl(myList)
## [1] 1 2 3

Solution 3 - R

This can be done by using unlist before as.vector. The result is the same as using the parameter use.names=FALSE.

as.vector(unlist(myList))

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsharozView Question on Stackoverflow
Solution 1 - RArunView Answer on Stackoverflow
Solution 2 - RhrbrmstrView Answer on Stackoverflow
Solution 3 - RcanevaeView Answer on Stackoverflow