On Dec 4, 2014, at 11:24 PM, Dirkse van Schalkwyk, Theuns <the...@sun.ac.za> 
wrote:

> 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)))))

The line above throws an error for too many closing parens, and when a paren is 
removed it then very reasonably complains about a missing value for "n". After 
a value of 3 is substituted for `n` the error becomes: Error: value for ‘u1’ 
not found. ( It's not clear what value the line provides since it is not 
assigned to any name.)


> 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]???

I get 17.

> 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

This would be much less painful if you built one list with named descendants. 
You can give character values that need to be evaluated to either "[[" or "["

routlist <- list( TimeB=numeric(), TimeE=numeric() )
> routlist[['TimeB']] <- lapply(paste0(Route1, "TimeB"), function(x) rep(0,N) )
> routlist[['TimeE']] <- lapply(paste0(Route1, "TimeB"), function(x) rep(0,N) )
> # Let's say the user input is to be placed in the 3rd location in TimeB's 
> first entry"
> inp_a <- "timeB"
> inc_b <- 400
> routlist[[inp_a]][[1]][3] <- inp_b
> routlist
$TimeB
$TimeB[[1]]
[1]   0   0 400   0   0   0   0   0   0

$TimeB[[2]]
[1] 0 0 0 0 0 0 0 0 0

$TimeB[[3]]
[1] 0 0 0 0 0 0 0 0 0

snipped remainder.

-- 
David.



> 
> 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]]
> 
> ______________________________________________
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius
Alameda, CA, USA

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to