简体   繁体   中英

How to unlist a nested named list without including prefix in R?

I have the following piece of code:

#example
test <- list("a" = 1, "b" = 2)
test2 <- list("x.y.z" = test)

unlist(test2["x.y.z"])

This resulting named vector is the following:

x.y.z.a     x.y.z.b 
1           2

However, I would like the result to not include the prefix of the nested list. It should be a named vector, but I don't want to include the information of how it was nested. The result I'm looking for:

a   b 
1   2

I understand that I can probably loop through each item and replace everything except the last character, but I think there should be a more efficient way. Thanks in advance.

Use [[

unlist(test2[["x.y.z"]])

#a b 
#1 2 

Or $

unlist(test2$x.y.z)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM