s-news
[Top] [All Lists]

Re: array with NA values

To: Tim Hesterberg <timh@insightful.com>
Subject: Re: array with NA values
From: Eric yang <yang_eric9@yahoo.com>
Date: Fri, 7 Apr 2006 04:59:48 -0700 (PDT)
Cc: s-news@lists.biostat.wustl.edu
Domainkey-signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=Message-ID:Received:Date:From:Subject:To:Cc:In-Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding; b=cmP27Xrzt8mkOmxByC28x11NOZxJBgYtcRkapvxjC53dYyNUrZXAAliLLJuiFiGWpa1w386mAS30mpoO4IJ+I+A9K4deX0Mbwxxe1b/AMSiaR3QmoNQl5wTIMCEagdi+qvVol8XA4HYaAGxRd9XhrCEhLv3PPHzFFu3AG7+oMv0= ;
In-reply-to: <SE2KEXCH01g6A7oPiFE0000110e@se2kexch01.insightful.com>

Thanks for the email Tim.

I forgot to mention that at the end of performing the calculations on the non-missing elements, I need to construct the original array which will be populated with the new numbers and all missing values remain the same.

Any help is appreciated.


Tim Hesterberg <timh@insightful.com> wrote:
My apologies; I used my own function "omit.na" in the example below.
Thanks to Chris Barker for noticing this.

omit.na <- function(x) x[!is.na(x)]

Tim Hesterberg


>If you are doing many operations with the same data, you might
>want to convert your data into a list with missing values omitted.
>Each element of the list would be a vector containing non-missing values.
>Optionally, you can give the list dimensions.
>
>x <- array(1:1000, c(10,10,10))
>set.seed(0)
>x[runif(1000) > .4] <- NA
>
>temp <- x
>dim(temp) <- c(10, prod(dim(x)[2:3]))
>x2 <- lapply(1:ncol(temp), function(j, x) omit.na(x[,j]), x = temp)
>x2 # list of length 100, containing non-missing values for each
>
># Now can turn x2 into a matrix/list hybrid if you like,
># to let you use apply on rows or columns
>dim(x2) <- dim(x)[2:3]
>
># example using apply:
>apply(x2, 2, function(x) length(unlist(x)))
>
>Tim Hesterberg
>
>>Dear all,
>>
>>I have numerous 3-dimenional arrays and each array contains no entire row or column with NA values. However, 50-60% of the values in the array are NA values. I perform various operations on the array using the apply statement. However, because the arrays are quite large the calculations take a while to finish. Within the apply statement I reduce the number of calculations on the vector by using the !is.na() statement. Is there a faster way to run the apply command without the need to read in an enitire vector from the array which contains a large amount of NA values, i.e. should I store the data in another format to avoid having an array with a large amount of NA values.
>>
>>For example, if I have a 3-dimensional 1000x1000x1000 and use the apply statement on dimensions 2 and 3, I'm reading in vectors of length 1000 in the apply statement and most of the 1000 values might be NAs. Can this be avoided?
>>
>>I would greatly appreciate any help on this.
>>Dave
>

--------------------------------------------------------------------
This message was distributed by s-news@lists.biostat.wustl.edu. To
unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
the BODY of the message: unsubscribe s-news


Blab-away for as little as 1¢/min. Make PC-to-Phone Calls using Yahoo! Messenger with Voice.
<Prev in Thread] Current Thread [Next in Thread>