[R] Using object as literal value in list vector

Rui Barradas ruipbarradas at sapo.pt
Wed Jun 20 15:14:35 CEST 2012


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.
>



More information about the R-help mailing list