Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
298 views
in Technique[技术] by (71.8m points)

r - Can lists be created that name themselves based on input object names?

It would be very helpful to me to be able to create an R list object without having to specify the names of each element. For example:

a1 <- 1
a2 <- 20
a3 <- 1:20

b <- list(a1,a2,a3, inherit.name=TRUE)
> b

[[a1]]
[1] 1

[[a2]]
[1] 20

[[a3]]
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20

This would be ideal. Any suggestions?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The tidyverse package tibble has a function that can do this as well. Try out tibble::lst

tibble::lst(a1, a2, a3)
# $a1
#  [1] 1
#
# $a2
#  [1] 20
# 
# $a3
#  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...