简体   繁体   中英

How to assign value to a list or vector object inside a vector in R

I have a list like

mylist = list('a','b','c','d')

and another vector inside which I wish to call mylist object to assign a value.

myvec = c('p'=0,'q'=1, mylist[1]=1)

But executing it gives error

Error: Unexpected '=' in "c('p'=0,'q'=1, mylist[1]="

I have used python before where such assignment is possible and straight forward. But same doesn't work in R. Any idea ?

You can use setNames() , also note that mylist[1] gives a one element list, in order to get the element at position one, you need to use [[ (which doesn't seem to matter in this case, ie, both works):

c('p' = 0,'q' = 1, setNames(1, mylist[[1]]))

#p q a 
#0 1 1 

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