Re: [R] how to assign a value to a specific position of a list

2017-05-01 Thread Jeff Newmiller
The only thing "compelling" about your example is that you have the pre-conceived (wrong) notion that you have to store your objects under separate names in your global environment. That is not only wrong, it handicaps you for future unified processing of these data. I see a lot of constructs

Re: [R] how to assign a value to a specific position of a list

2017-05-01 Thread Jinsong Zhao
Thank you very much, and your reply is helpful. I don't like assign, and even don't use parse in my previous codes. However, in the case I encountered, assign and parse may be the right tools. Here is the code I used: # in the workspace, there are tens directory. # In each directory, there ar

Re: [R] how to assign a value to a specific position of a list

2017-05-01 Thread MacQueen, Don
It's not clear what you're trying to do. However, to "assign a value to a specific position of a list", this example should show you how. lst <- vector('list', 10) ## see the help page for list names(lst) <- paste0('list.',1:10) ## to assign 'a' to position 3: pos <- 3 lst[[pos]] <- 'a' I

Re: [R] how to assign a value to a specific position of a list

2017-04-30 Thread Bert Gunter
... and moreover, note that the assignment can even be shortened to: > for ( i in 1:10 ) l[[c(i,1)]] <- 5 ?"[[" contains details, but the relevant point is: "[[ can be applied recursively to lists, so that if the single index i is a vector of length p, alist[[i]] is equivalent to alist[[i1]].

Re: [R] how to assign a value to a specific position of a list

2017-04-30 Thread Jeff Newmiller
My reaction is... why do you think this is a good approach to pursue? Avoid using assign! library( fortunes ) fortune( 236 ) If you really need another level of containment, put your multiple lists into another list: lst <- lapply( 1:10, list ) lst[[1]][[1]] <- 5 -- Sent from my phone. Plea

Re: [R] how to assign a value to a specific position of a list

2017-04-30 Thread peter dalgaard
assign(paste("list_", i, "[[1]]", sep = ""), 5) creates a new variable with a funny name. You'd have to parse() and eval() to make that work, something like eval(parse(text=paste("list_",i,"[[1]]<-",5, sep=""))) However, --- > fortunes::fortune("parse") If the answer is parse() you should

Re: [R] how to assign a value to a specific position of a list

2017-04-30 Thread Jinsong Zhao
On 2017/4/30 23:17, Jinsong Zhao wrote: Hi there, I have a problem with assign(). Here is the demo code: for (i in 1:10) { # create a list with variable name as list_1, list_2, ..., etc. assign(paste("list_", i, sep = ""), list()) # I hope to assign 5 to list_?[[1]], but I don't know h