[R-sig-Geo] masking z values (extract or subset on a 3-d array)

Andy Bunn abunn at nova.whrc.org
Sat May 27 20:57:21 CEST 2006


>Andy,
>
>Maybe this is confusing because the data really ought to be in a dataframe
>with factors coding for treatments?

Can't argue with that logic...it's the principle of the thing you understand. 

>
>Here's one solution:
>
>foo[,'z',] <- sapply(foo[,'z',], function(x) max(c(x, 0)))
>

Thanks! -A


>THK
>
>On 5/26/06, Andy Bunn <abunn at whrc.org> wrote:
>>
>> > > This is driving me nuts. I have a 3-D array that holds x,y,z
>> > > coordinates from multiple experiments. I need to clean the
>> > data before
>> > > making it sp objects. I want to set all z-values < 0 to 0. In a 2-D
>> > > matrix this is trivial e.g., x[,x[1,]<0] <- 0. But I'm
>> > stumped on how
>> > > to do this on a 3-D array without a loop. Given an array:
>> > >
>> > >   foo <- array(rnorm(24), dim=c(4,3,2))
>> > >   dimnames(foo) <-
>> > list(NULL,c('x','y','z'),c('Experiment1','Experiment2'))
>> > >   foo[,'z',]
>> > >
>> > > How can I set foo[,'z',]<0 to 0?
>> >
>> > a <- array(rnorm(64), c(4,4,4))
>> > inds <- which(a < 0, arr.ind=TRUE)
>> > a[inds] <- 0
>> >
>>
>> Thanks, but I'm looking to set only the values from one dimension to zero.
>> As above, I just want to set the 'z' values to 0 if they are less than
>> one.
>> This works:
>>
>>   foo <- array(rnorm(24), dim=c(4,3,2))
>>   dimnames(foo) <-list(NULL,c('x','y','z'),c('Experiment1','Experiment2'))
>>   foo[,'z',]
>>   #How can I set foo[,'z',]<0 to 0
>>   for(i in 1:2){
>>     foo[foo[,'z',i]<0,'z',i] = 0
>>   }
>>   foo
>>
>> But it seems like one should be able to do this with apply or just '['.
>> I'm
>> at a loss.
>>
>> -A
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo at stat.math.ethz.ch
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>
>
>
>
>-- 
>Timothy H. Keitt
>http://www.keittlab.org/
>
>




More information about the R-sig-Geo mailing list