Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a variable name with "paste" in R?

Tags:

r

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 
like image 822
qed Avatar asked Apr 01 '11 08:04

qed


People also ask

How do I create a variable name in R?

Variable Names Rules for R variables are: A variable name must start with a letter and can be a combination of letters, digits, period(.) and underscore(_). If it starts with period(.), it cannot be followed by a digit.

How do you assign a name to something in R?

names() function in R Language is used to get or set the name of an Object. This function takes object i.e. vector, matrix or data frame as argument along with the value that is to be assigned as name to the object. The length of the value vector passed must be exactly equal to the length of the object to be named.

Can variable name start with underscore in R?

Underscore('') at the beginning of the variable name are not allowed.


1 Answers

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

> assign(paste("perf.a", "1", sep=""),5) > perf.a1 [1] 5 
like image 62
lecodesportif Avatar answered Sep 28 '22 00:09

lecodesportif