[R] Can I write Nothing to a variable?
Carl Witthoft
carl at witthoft.com
Fri Oct 30 16:56:47 CET 2009
OK, here's the first-cut code, which does execute properly. What I
would like to do, simply for 'cleanliness', is to assign "nothing" to
the variable "l" , rather than assigning "seq(1,dim(data)[timedim])".
Then the result of all the get(unlist(dims)) operations below will be
func[j,k,]
Where right now the code returns func[j,k,seq(1:length(dim(data)[timedim]))]
Linewraps below have broken the definition of jmap() into at least 3
lines, btw.
What this function, timecalc(), does is to calculate the selected 'func'
over the specified dimension of the data, returning a 2-dimensional
array of the calculated values. Think, for example, of a FITS data
stack, and calculating the mean intensity in each spatial pixel over the
full time-sequence of frames.
************
#generic version, for 'any' function and any axis (3-D data only)
timecalc<-function( data, func=mean, timedim=3) {
# build a "[,j,k]" string where the blank is for
# the timedim selected. I can't find a way to create a true "blank"
# which is why I load the seq(1:N) in there
# (put error checkers on length of dim later)
dims<-as.list(dim(data))
dims[-timedim]<-c('j','k')
#sloppy way to extract the time-sequence, but lets me 'unlist' all 3 dims
dims[[timedim]]<-'l'
l<-seq(1,dim(data)[timedim])
alldim<-seq(1,3) # just create a holder
# define the two dims which are NOT summed over, i.e. we'll return an array
# of size scandim[1]xscandim[2], each element is func-ed over 3rd dim
scandims<-alldim[alldim!=timedim]
funcname<-deparse(substitute(func))
datname<-deparse(substitute(data))
jmap<-function(k) mapply(function(j)
eval(call(funcname,get(datname)[get(unlist((dims[1]))),get(unlist(dims[2])),get(unlist(dims[3]))])),
j=seq(1:dim(data)[scandims[1]]))
mapply(jmap,k=seq(1:dim(data)[scandims[2]]))->bar
return(invisible(bar))
}
*******************
David Winsemius wrote:
> It certainly seems possible that it is more complex, but at the moment I
> don't think it is possible to any more vague.
>
> The specifics of implementation will depend on the precise meaning
> assigned to the words "a function", "index", "axis", "dimension", "each
> element of which is the sum of all values along the third axis". At the
> moment those appear to be unfortunately imprecisely described.
>
> The quick answer to your first questions is yes, it is possible to
> create structures with nothing in them. Emply vectors, empty arrays,
> sparse matrices, and empty lists are all feasible. Make up your mind
> what you want and then articulate it.
>
> some the the function sthat may do what you want are:
>
> apply
> integrate
> lapply
>
> All of the specifics depend ... on specifics.
>
More information about the R-help
mailing list