r - Assigning to a dynamically created vector -
how assign dynamically created vector?
master<-c("bob","ed","frank") d<-seq(1:10) (i in 1:length(master)){ assign(master[i], d ) } eval(parse(text=master[2]))[2] # can access data # how can assign returns error ####################### eval(parse(text=master[2]))[2]<- 900
ok. i'll post code because asked to:
> eval(parse(text=paste0( master[2], "[2]<- 900" ) ) ) > ed [1] 1 900 3 4 5 6 7 8 9 10
it's considered bad practice use such method. need build expression" ed[2] < 100
. using paste0
lets evaluate master[2] 'ed'
concatenated rest of characters before passing parse
convert language object. more in keeping considered better practice:
master<-c("bob","ed","frank") d<-seq(1:10) mlist <- setnames( lapply(seq_along(master), function(x) {d} ), master)
so changing second value of second item <-
:
> mlist[[2]][2] <- 900 > mlist[['ed']] [1] 1 900 3 4 5 6 7 8 9 10
Comments
Post a Comment