Just wondering if it's possible to have an item with no content at all. Here's what I was hoping to do, inside a larger function that acts on a 3-D array. I want to enter, as one of the arguments to the main function, the index over which I'm going to do some action. For example, if the action were to calc a sum, I'd specify a given axis and report the sums along that axis for all coordinate pairs on the other two axes of the array. So, I'd hoped to define three variables sort of like A1 = 'i' A2 = 'j' A3 = {nothing} then a loop over i and j around a command something like (not the real code, but you get the idea): > sum(thearray[get(A1),get(A2),get(A3)]) which in my optimistic mind would turn into "sum(thearray[i,j,])" . The problem is that I don't see any way to have nothing whatsoever in the selected index. A value of A3='' gets interpreted as a 'blank' name or some such. I solved the problem by setting the value to A3 = 'seq(1,dim(thearray)[3])' (or similar), but just would like to know in general if there is some way to put nothing at all in there. (NA and NULL don't work) Carl
On Oct 28, 2009, at 5:22 PM, Carl Witthoft wrote:> Just wondering if it's possible to have an item with no content at > all. Here's what I was hoping to do, inside a larger function that > acts on a 3-D array. > > I want to enter, as one of the arguments to the main function, the > index over which I'm going to do some action. For example, if the > action were to calc a sum, I'd specify a given axis and report the > sums along that axis for all coordinate pairs on the other two axes > of the array. > > So, I'd hoped to define three variables sort of like > > A1 = 'i' > A2 = 'j' > A3 = {nothing} >Forget the variables,,,,> then a loop over i and j around a command something like (not the > real code, but you get the idea):... and forget the loops.> > > > sum(thearray[get(A1),get(A2),get(A3)]) > which in my optimistic mind would turn into "sum(thearray[i,j,])"> smlarr <- array(1:27, c(3,3,3)) > smlarr , , 1 [,1] [,2] [,3] [1,] 1 4 7 [2,] 2 5 8 [3,] 3 6 9 , , 2 [,1] [,2] [,3] [1,] 10 13 16 [2,] 11 14 17 [3,] 12 15 18 , , 3 [,1] [,2] [,3] [1,] 19 22 25 [2,] 20 23 26 [3,] 21 24 27 > sum( smlarr[1:2, 2, ] ) # could use any named or unnamed vectors whose range is within that defined for the array #[1] 81> > . The problem is that I don't see any way to have nothing > whatsoever in the selected index. A value of A3='' gets interpreted > as a 'blank' name or some such. > > I solved the problem by setting the value to > > A3 = 'seq(1,dim(thearray)[3])' (or similar), > > but just would like to know in general if there is some way to put > nothing at all in there. (NA and NULL don't work)-- David Winsemius, MD Heritage Laboratories West Hartford, CT
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. >