How to add variable key/value pair to list object?

前端 未结 4 1755
粉色の甜心
粉色の甜心 2020-12-04 19:36

I have two variables, key and value, and I want to add them as a key/value pair to a list:

key = \"width\"
value = 32

mylist = lis         


        
4条回答
  •  粉色の甜心
    2020-12-04 19:51

    List elements in R can be named. So in your case just do

     > mylist = list()
     > mylist$width = value
    

    When R encounters this code

    > l$somename=something
    

    where l is a list. It appends to a list an element something, and names it with name somename. It is then can be accessed by using

    > l[["somename"]]
    

    or

    > l$somename
    

    The name can be changed with command names:

    > names(l)[names(l)=="somename"] <- "othername"
    

    Or if you now the position of the element in the list by:

    > names(l)[1] <- "someothername"
    

提交回复
热议问题