[R] Variable String formation

David Winsemius dwinsemius at comcast.net
Thu Sep 22 20:21:15 CEST 2016


> 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



More information about the R-help mailing list