Jinsong Zhao
2017-Apr-30 15:17 UTC
[R] how to assign a value to a specific position of a list
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 how to code it. # list_1[[1]] <- 5 # works, however assign(paste("list_", i, "[[1]]", sep = "", 5) # does not work } How to do? Is there any alternatives? Many thanks! Best, Jinsong
Jinsong Zhao
2017-Apr-30 15:20 UTC
[R] how to assign a value to a specific position of a list
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 how to code it. > # list_1[[1]] <- 5 # works, however > assign(paste("list_", i, "[[1]]", sep = "", 5) # does not work# wrong code in previous message, the correct on should be: assign(paste("list_", i, "[[1]]", sep = ""), 5) # does not work...> } > > How to do? Is there any alternatives? Many thanks! > > Best, > Jinsong
peter dalgaard
2017-Apr-30 15:48 UTC
[R] how to assign a value to a specific position of a list
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 usually rethink the question. -- Thomas Lumley R-help (February 2005) ------- It is much easier to handle this using a data structure containing a list of lists: l <- rep(list(list()), 10) for ( i in 1:10 ) l[[i]][[1]] <- 5> On 30 Apr 2017, at 17:17 , Jinsong Zhao <jszhao at yeah.net> 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 how to code it. > # list_1[[1]] <- 5 # works, however > assign(paste("list_", i, "[[1]]", sep = "", 5) # does not work > } > > How to do? Is there any alternatives? Many thanks! > > Best, > Jinsong > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Office: A 4.23 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
Jeff Newmiller
2017-Apr-30 15:56 UTC
[R] how to assign a value to a specific position of a list
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. Please excuse my brevity. On April 30, 2017 8:20:17 AM PDT, Jinsong Zhao <jszhao at yeah.net> wrote:>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 how to code >it. >> # list_1[[1]] <- 5 # works, however >> assign(paste("list_", i, "[[1]]", sep = "", 5) # does not work > # wrong code in previous message, the correct on should be: > assign(paste("list_", i, "[[1]]", sep = ""), 5) # does not work... >> } >> >> How to do? Is there any alternatives? Many thanks! >> >> Best, >> Jinsong > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide >http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code.
Bert Gunter
2017-Apr-30 17:14 UTC
[R] how to assign a value to a specific position of a list
... 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]]...[[ip]] providing all but the final indexing results in a list." For a less terse version, see any good online R tutorial. Lists are extremely useful in R, and indexing is fundamental. If you haven't spent the time to learn about these constructs, you should now before posting further. You'll save yourself a lot of grief and perhaps even some embarassment. Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Sun, Apr 30, 2017 at 8:48 AM, peter dalgaard <pdalgd at gmail.com> wrote:> 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 usually rethink the question. > -- Thomas Lumley > R-help (February 2005) > ------- > > It is much easier to handle this using a data structure containing a list of lists: > > l <- rep(list(list()), 10) > for ( i in 1:10 ) > l[[i]][[1]] <- 5 > >> On 30 Apr 2017, at 17:17 , Jinsong Zhao <jszhao at yeah.net> 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 how to code it. >> # list_1[[1]] <- 5 # works, however >> assign(paste("list_", i, "[[1]]", sep = "", 5) # does not work >> } >> >> How to do? Is there any alternatives? Many thanks! >> >> Best, >> Jinsong >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. > > -- > Peter Dalgaard, Professor, > Center for Statistics, Copenhagen Business School > Solbjerg Plads 3, 2000 Frederiksberg, Denmark > Phone: (+45)38153501 > Office: A 4.23 > Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
MacQueen, Don
2017-May-01 16:35 UTC
[R] how to assign a value to a specific position of a list
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 completely agree with Jeff Newmiller's recommendation to avoid using assign. It's probably the wrong tool for what you're trying to do (whatever that is). (and note that I have borrowed Jeff's name "lst" for the list with 10 elements [not variables] whose names are "list_1" "list_2" etc.) (and I refuse to use "_" in R object names, but that's a personal preference) -Don -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 On 4/30/17, 8:17 AM, "R-help on behalf of Jinsong Zhao" <r-help-bounces at r-project.org on behalf of jszhao at yeah.net> 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 how to code it. # list_1[[1]] <- 5 # works, however assign(paste("list_", i, "[[1]]", sep = "", 5) # does not work } How to do? Is there any alternatives? Many thanks! Best, Jinsong ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Jinsong Zhao
2017-May-02 00:37 UTC
[R] how to assign a value to a specific position of a list
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 are lots of *.ASC file, # with second column is the data. # Each *.ASC file has a name with pattern i-tr-??.ASC. # i is the directory name, tr is a group name, and ?? are the index. # I have to collect all tr-?? into a matrix, # and put all i-tr-?? into a list EEM_i. for (i in dir()) { assign(paste("EEM_",i,sep=""), list()) fn <- list.files(path = i, pattern = "ASC") tr <- sort(as.numeric(unique(unlist(lapply(strsplit(fn, "-"),"[[", 2))))) for (j in 1:length(tr)) { fn_tr <- list.files(path = i, pattern = paste(i, tr[j], "...ASC", sep="-")) EEM_tmp <- matrix(NA,ncol = length(fn_tr),nrow = 371) for (k in 1:length(fn_tr)) { data_tmp <- read.csv(paste(i,fn_tr[k],sep="/"), header = FALSE) if (dim(data_tmp)[1] != 371) next EEM_tmp[,k] <- data_tmp[,2] } eval(parse(text=paste("EEM_",i,"[[",j,"]]<-","EEM_tmp", sep=""))) } } Any alternatives or improvements? Thanks a lot. Best, Jinsong On 2017/4/30 23:48, peter dalgaard wrote:> 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 usually rethink the question. > -- Thomas Lumley > R-help (February 2005) > ------- > > It is much easier to handle this using a data structure containing a list of lists: > > l <- rep(list(list()), 10) > for ( i in 1:10 ) > l[[i]][[1]] <- 5 > >> On 30 Apr 2017, at 17:17 , Jinsong Zhao <jszhao at yeah.net> 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 how to code it. >> # list_1[[1]] <- 5 # works, however >> assign(paste("list_", i, "[[1]]", sep = "", 5) # does not work >> } >> >> How to do? Is there any alternatives? Many thanks! >> >> Best, >> Jinsong >>