0 votes
1 view
in R Programming by (7.8k points)

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

1 Answer

0 votes
by (25.3k points)

You can use the assign() function to assign a value to a name in an environment.

According to R Documentation:

Usage

assign(x, value, pos = -1, envir = as.environment(pos),

       inherits = FALSE, immediate = TRUE)

Arguments

x

A variable name, given as a character string. No coercion is done, and the first element of a character vector of length greater than one will be used, with a warning.

value

A value to be assigned to x.

pos

Where to do the assignment. By default, assigns into the current environment. 

envir

The environment to use.

inherits

Should the enclosing frames of the environment be inspected?

immediate

An ignored compatibility feature.

In your case:

assign(paste("perf.a", "1", sep=""),5)

perf.a1

[1] 5

Related questions

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...