[R] Odp: assign names to vectors in loop
Petr PIKAL
petr.pikal at precheza.cz
Tue Aug 9 13:18:28 CEST 2011
Hi
> Dear List,
> I like to assign names to vectors in a loop.
> Here is a short example:
> DMUs <-
>
as.data.frame(matrix(c("b","c","d","a","e","h","i","f","g","j","k","l"),ncol
> =7, nrow=10))
>
> colnames(DMUs) <- v_DMUs <- c("a","b","c","d","e","f","g")
>
> for (i in v_DMUs)
>
> assign(paste("eff_val",i,sep="."),abs(rnorm(10)))
>
> Now I like to find a solution for applying following operation in a loop
for
> all vectors eff_val.i (with i = a:g) and names in columns (columns i =
a:g)
> of dataframe DMUs. Perhaps there is a solution without loop, too?!
>
> names(eff_val.a) <- t(subset(DMUs, select="a"))
>
> I tried the following. But this results in an error-message!
>
> for (i in v_DMUs)
>
> names(paste("eff_val.",i,sep="")) <- t(subset(DMUs,select =
> paste("",i,sep="")))
>
>
> I don't know how to beat this bastard ;-)
Pardon me that I do not solve your problem in suggested way. I am not sure
what you really want to do but seems to me that keeping your values in
list could be faR betteR way.
#So i first made a vector from eff_val.x values
vec<-NULL
for (i in v_DMUs) vec<-c(vec,get(paste("eff_val.",i,sep="")))
#transform DMUs back to matrix
mat<-as.matrix(DMUs)
#got rid of dimensons
dim(mat)<-NULL
#put names from mat and values from vec alongside to data frame
result<-data.frame(name=mat, num=vec)
#split the result to those seven chunks
split(result, rep(1:7, each=10)
I am almost sure that it is not what you was about but I feel that you
shall give it a thought.
Lists are much more easier to handle with R then many variables with
yy.xx.i names.
Regards
Petr
>
> Thanks for your support.
> Henning
> ______________________________________________
> 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