0

I'm trying to create variables with their name dependent on a vector

For example I want to generate a variable name called P_00 but the 0's are actually elements of a vector

# Vector with two positions at 0: v[1] = 0, v[2] = 0
v = rep(0, 2)

# Create variable P_00 and assign 0.75
Pv[1]v[2] = .75

where v[1]=0 and v[2]=0 so if I were to call P_00 it would display .75

coatless
  • 18,823
  • 13
  • 63
  • 79

1 Answers1

2

The assign function should work for this, for example:

v <- c(0, 0)
val <- 0.75
assign(sprintf('P_%d%d', v[1], v[2]), val)

> P_00
[1] 0.75

More info here.

Keith Hughitt
  • 4,571
  • 5
  • 44
  • 54