See below:

paste("perf.a", "1", sep="") # [1] "perf.a1" 

What if I want to assign a value to perf.a1?

I tried as.name, as.symbol, etc., with no avail:

as.name(paste("perf.a", "1", sep="")) = 5 # Error in as.name(paste("perf.a", "1", sep = "")) = 5 : # target of assignment expands to non-language object as.symbol(paste("perf.a", "1", sep="")) = 5 # Error in as.symbol(paste("perf.a", "1", sep = "")) = 5 : # target of assignment expands to non-language object noquote(paste("perf.a", "1", sep="")) = 5 # Error in noquote(paste("perf.a", "1", sep = "")) = 5 : # target of assignment expands to non-language object 
3

4 Answers

You can use assign (doc) to change the value of perf.a1:

> assign(paste("perf.a", "1", sep=""),5) > perf.a1 [1] 5 
2

See ?assign.

> assign(paste("tra.", 1, sep = ""), 5) > tra.1 [1] 5 

In my case function eval() works very good. Below I generate 10 variables and assign them 10 values.

lhs <- rnorm(10) rhs <- paste("perf.a", 1:10, "<-", lhs, sep="") eval(parse(text=rhs)) 
1

In my case the symbols I create (Tax1, Tax2, etc.) already had values but I wanted to use a loop and assign the symbols to another variable. So the above two answers gave me a way to accomplish this. This may be helpful in answering your question as the assignment of a value can take place anytime later.

output=NULL for(i in 1:8){ Tax=eval(as.symbol(paste("Tax",i,sep=""))) L_Data1=L_Data_all[which(L_Data_all$Taxon==Tax[1] | L_Data_all$Taxon==Tax[2] | L_Data_all$Taxon==Tax[3] | L_Data_all$Taxon==Tax[4] | L_Data_all$Taxon==Tax[5]),] L_Data=L_Data1$Length[which(L_Data1$Station==Plant[1] | L_Data1$Station==Plant[2])] h=hist(L_Data,breaks=breaks,plot=FALSE) output=cbind(output,h$counts) } 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy