[R] Can I write Nothing to a variable?

David Winsemius dwinsemius at comcast.net
Wed Oct 28 22:57:48 CET 2009


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




More information about the R-help mailing list