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. Thanks, -Roy ********************** "The contents of this message do not reflect any position of the U.S. Government or NOAA." ********************** Roy Mendelssohn Supervisory Operations Research Analyst NOAA/NMFS 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.
> On Sep 22, 2016, at 8:01 AM, Roy Mendelssohn - NOAA Federal <roy.mendelssohn at noaa.gov> wrote: > > 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. >Take a look at this function to see if it allows you to make this cleaner: ?R.utils::extract.array (Author = Henrik Bengtsson; so you have can have confidence in its quality.) Found with the ever-useful `findFn` function: findFn("extract slice array") found 28 matches; retrieving 2 pages 2 Downloaded 28 links in 24 packages. To whose author/maintainer I give almost daily silent thank yous: maintainer('sos') [1] "Spencer Graves <spencer.graves at prodsyse.com>"> Thanks, > > -Roy > >David Winsemius Alameda, CA, USA