Nneka
Nneka

Reputation: 1860

referring to an object with a space in R

I have created a nested list:

my_list <- list(A = "first item", 
                B = "second item")

and given this list the name 'list 1':

vector_names <- c("list 1", "list 2")
assign(vector_names[[1]], my_list)

However, now I want to use the object list 1 inside of a nested list, to obtain:

final_list <- list("1st_list" = list(A = "first item", 
                                     B = "second item"))

How can I refer to the list without copying it again? I tried:

final_list <- list("1st_list" = 'list 1')

but this doesn't return the Object list 1, it return a character vector 'list 1'.

Upvotes: 1

Views: 223

Answers (1)

akrun
akrun

Reputation: 887501

As there is space in object names, use backquote instead of single/double quote

list("1st_list" = `list 1`)

-output

$`1st_list`
$`1st_list`$A
[1] "first item"

$`1st_list`$B
[1] "second item"

Upvotes: 3

Related Questions