[R] Variable String formation

Roy Mendelssohn - NOAA Federal roy.mendelssohn at noaa.gov
Thu Sep 22 17:01:49 CEST 2016

Hi All:

I am trying to write code to create a string to be executed as a command.  The string will be of the form:

"param <- param[,rev(seq_len(dataYLen)),,drop = FALSE]"

Now just creating that string is simple enough.  Where the problem arises is the array param could be 2, 3, or 4 dimensions, and the dimension where  "rev(seq_len(dataYLen))" occurs can vary.  At present I have the following solution:

      paramLen <-  3
      latLoc <- 2
      myComma1 <- paste(rep(',', times = (latLoc-1)), 'rev(seq_len(dataYLen))', sep="", collapse="")
      myComma2 <- paste(rep(',', times = (paramLen-latLoc+1)),sep="", collapse="")
      paramCommand <- paste0('param <- param[', myComma1, myComma2, 'drop = FALSE]')

(paramLen can be 2,3,4 and latLoc can be 1,2,3,4)  but this strikes me as pretty kludgy.  I am hoping there is a more elegant way of doing this.



"The contents of this message do not reflect any position of the U.S. Government or NOAA."
Roy Mendelssohn
Supervisory Operations Research Analyst
Environmental Research Division
Southwest Fisheries Science Center
***Note new address and phone***
110 Shaffer Road
Santa Cruz, CA 95060
Phone: (831)-420-3666
Fax: (831) 420-3980
e-mail: Roy.Mendelssohn at noaa.gov www: http://www.pfeg.noaa.gov/

"Old age and treachery will overcome youth and skill."
"From those who have been given much, much will be expected" 
"the arc of the moral universe is long, but it bends toward justice" -MLK Jr.

More information about the R-help mailing list