[R] I am trying to use assign and paste to assign value to existing variable

Dirkse van Schalkwyk, Theuns <theuns@sun.ac.za> theuns at sun.ac.za
Fri Dec 5 08:24:17 CET 2014


In the code below, the last line of code does what I am trying to do; however, I do not know the name of the variable before the user creates it, by choosing values in Route1. So, how can I assign values to the variables individually, when I created them using the code in lines 9-12 and assigned values in lines 15 and 16?

Lines 18 to 24 are various ways that I tried without success. Line 26 does what I want of course, but since I don't know the name of the variable beforehand I cannot use it. Maybe it will be easier to re-think the problem to use a dataframe; but I would still like to know how to do this as well.


evaluate<-function(..., envir=.GlobalEnv){ eval(parse(text=paste( ... ,sep="")), envir=envir) }  #By Rufo in stackoverflow
envir <- environment()

Route1<<- c("Ao1","B1","C1","D1","Ei1")
Arrive<<- c(15,30,100,1000,5000,12000)   # the time between events > exponential parameter of the arrivals
N <<- 9  # number of simulated arrivals
Route1S<<- length(Route1)    # determines the number of routes in Route1

for (i in 1:(Route1S)){     # create the route variables, maybe can be vectorised, but this works for now...
  assign(paste(Route1[i],"TimeB",sep = ""), rep(0, N))
  assign(paste(Route1[i],"TimeE",sep = ""), rep(0, N))
  assign(paste(Route1[i],"ServiceT",sep = ""), rep(0, N))
}

assign(paste(Route1[1],"TimeB",sep = ""), round(cumsum(rpois(N,Arrive[1])),2))  #time the entity comes into Source1, N values
assign(paste(Route1[1],"ServiceT",sep = ""),  round(rpois(N,Arrive[2]),2)) #just service time

assign(eval(paste(Route1[1],"TimeB[",2,"]",sep = "")), 3)
x[1]<-0
assign(paste(Route1[1],"TimeB",eval("[1]"),sep = ""), 0)
sum(unlist(mget(paste("u",1:n,sep=""),envir=as.environment(-1)))))
evaluate(paste(Route1[1],"TimeB[1]",sep = ""), envir=envir) # will find the value in Ao1TimeB[1], but how to place a new value in Ao1TimeB[1]???
assign(evaluate(paste(Route1[1],"TimeB[1]",sep = "")), 0)   # Creates Ao1Timeb[1] as 0 but does not change the vector Ao1TimeB...
assign(eval(parse(text=paste(Route1[1],"TimeB[1]",sep = ""))), 0)  # does not work either and is frowned upon R-help list 106

Ao1TimeB[1]<- 0   # this is what I am trying to do in the previous two lines
# the reason I want to do it with the paste method is because there could be 100 values in Route1
# and expanding the method to include more such as Route2 with another 100 values etc.
# I am trying to figure out how to address these values Ao1TimeB[1], Ao1TimeB[2] etc without typing the variable name

Theuns
The integrity and confidentiality of this email is governed by these terms / Hierdie terme bepaal die integriteit en vertroulikheid van hierdie epos. http://www.sun.ac.za/emaildisclaimer

The integrity and confidentiality of this email is governed by these terms / Hierdie terme bepaal die integriteit en vertroulikheid van hierdie epos. http://www.sun.ac.za/emaildisclaimer



	[[alternative HTML version deleted]]



More information about the R-help mailing list