2

Consider the following code

names = c("1","2","3")
values = c(1.0,1.5,0.5)
list(names = values)

This produces the following list

$names
[1] 1.0 1.5 0.5

But the list I desire is:

$`1`
[1] 1

$`2`
[1] 1.5

$`3`
[1] 0.5

In other words, list("1"=1.0,"2"=1.5,"3"=0.5)

So, how can I create such a list by starting with variables names and values as above?

2 Answers 2

4

You can use setNames and as.list:

setNames(as.list(values), names)
# $`1`
# [1] 1
#
# $`2`
# [1] 1.5
# 
# $`3`
# [1] 0.5
Sign up to request clarification or add additional context in comments.

Comments

2
Map(function(x,y) y,names,values)

$`1`
[1] 1

$`2`
[1] 1.5

$`3`
[1] 0.5

1 Comment

You actually only need Map(function(x,y) y, names, values)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.