Hello all , This is a pretty simple question I think but cannot find an answer on the list or in my brain. I would like to iterate through a loop and use a vector of strings to name a number of list elements. For instance #Create vector of strings Et<- c("ACC","RTL","WHL") MeanValues_ <- list("ACC" = 1000, "RTL" = 2000, "WHL" = 3000) #Iterate through each element of vector NewMeans_ <- list() for(et in Et){ NewMeans_ <- c(NewMeans_,list(et = unlist(MeanValues_[et]) * .80 )) } Returns> NewMeans_$et ACC 800 $et RTL 1600 $et WHL 2400 But I want 'et' to be the object value of et, so it would be $ACC, $RTL, and $WHL. I realize there may be another way to actually do what I want to do as far as applying the calculation but I have a bunch of code structured around the need for my list coming in a certain way. Thanks Josh -- View this message in context: http://r.789695.n4.nabble.com/Using-object-as-literal-value-in-list-vector-tp4633912.html Sent from the R help mailing list archive at Nabble.com.
Hello, Try NewMeans_ <- lapply(MeanValues_, `*`, 0.80) That's it. Note also that you could have used, after your loop, names(NewMeans_) <- Et But 'lapply' is much better. Hope this helps, Rui Barradas Em 20-06-2012 01:31, LCOG1 escreveu:> Hello all , > This is a pretty simple question I think but cannot find an answer on the > list or in my brain. I would like to iterate through a loop and use a > vector of strings to name a number of list elements. For instance > > #Create vector of strings > Et<- c("ACC","RTL","WHL") > MeanValues_ <- list("ACC" = 1000, "RTL" = 2000, "WHL" = 3000) > > #Iterate through each element of vector > NewMeans_ <- list() > for(et in Et){ > NewMeans_ <- c(NewMeans_,list(et = unlist(MeanValues_[et]) * .80 )) > } > > Returns >> NewMeans_ > $et > ACC > 800 > > $et > RTL > 1600 > > $et > WHL > 2400 > > But I want 'et' to be the object value of et, so it would be $ACC, $RTL, and > $WHL. I realize there may be another way to actually do what I want to do > as far as applying the calculation but I have a bunch of code structured > around the need for my list coming in a certain way. Thanks > > Josh > > -- > View this message in context: http://r.789695.n4.nabble.com/Using-object-as-literal-value-in-list-vector-tp4633912.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > 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. >
Yes I feel foolish as this was the solution I finally came up with. It came to me in the shower, once I was able to pull myself out of the code and think for a while. Thanks for you insights JR -----Original Message----- From: arun [mailto:smartpink111 at yahoo.com] Sent: Wednesday, June 20, 2012 1:14 PM To: ROLL Josh F Cc: R help Subject: Re: [R] Using object as literal value in list vector Hi, You can try: NewMeans<-split(matrix(unlist(MeanValues_)*0.80),1:nrow(matrix(unlist(MeanValues_)*0.80))) names(NewMeans)<-names(MeanValues_) NewMeans $ACC [1] 800 $RTL [1] 1600 $WHL [1] 2400 A.K. ----- Original Message ----- From: LCOG1 <jroll at lcog.org> To: r-help at r-project.org Cc: Sent: Tuesday, June 19, 2012 8:31 PM Subject: [R] Using object as literal value in list vector Hello all , ? This is a pretty simple question I think but cannot find an answer on the list or in my brain.? I would like to iterate through a loop and use a vector of strings to name a number of list elements.? For instance ? ? ? #Create vector of strings ??? Et<- c("ACC","RTL","WHL") ??? MeanValues_ <- list("ACC" = 1000, "RTL" = 2000, "WHL" = 3000) ??? #Iterate through each element of vector ??? NewMeans_ <- list() ??? for(et in Et){ ??? ??? NewMeans_ <- c(NewMeans_,list(et = unlist(MeanValues_[et]) * .80 )) ??? } Returns> NewMeans_$et ACC 800 $et RTL 1600 $et WHL 2400 But I want 'et' to be the object value of et, so it would be $ACC, $RTL, and $WHL.? I realize there may be another way to actually do what I want to do as far as applying the calculation but I have a bunch of code structured around the need for my list coming in a certain way.? Thanks Josh -- View this message in context: http://r.789695.n4.nabble.com/Using-object-as-literal-value-in-list-vector-tp4633912.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help at r-project.org mailing list 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.