[R] Selecting cases from matrices stored in lists
Gabor Grothendieck
ggrothendieck at gmail.com
Mon Aug 22 17:01:28 CEST 2011
On Mon, Aug 22, 2011 at 9:07 AM, Jean V Adams <jvadams at usgs.gov> wrote:
>> [R] Selecting cases from matrices stored in lists
>> mdvaan
>> to:
>> r-help
>> 08/22/2011 07:24 AM
>>
>> Hi,
>>
>> I have two lists (c and h - see below) containing matrices with similar
>> cases but different values. I want to split these matrices into multiple
>> matrices based on the values in h. So, I did the following:
>>
>> years<-c(1997:1999)
>> for (t in 1:length(years))
>> {
>> year=as.character(years[t])
>> h[[year]]<-sapply(colnames(h[[year]]), function(var)
>> h[[year]][h[[year]][,var]>0, h[[year]][var,]>0])
>> }
>>
>> Now that I have created list h (with split matrices), I would like to
> use
>> these selections to make similar selections in list c. List c needs to
> get
>> the exact same shape as h, so that `8026`in 1997 (c$`1997`$`8026`) looks
>> like this:
>>
>> $`1997`$`8026`
>> B
>> B 8025 8026 8029
>> 8025 1.0000000 0.7739527 0.9656091
>> 8026 0.7739527 1.0000000 0.7202771
>> 8029 0.9656091 0.7202771 1.0000000
>>
>> Can anyone help me doing this? I have no idea how I can get it to work.
>> Thank you very much for your help!
>>
>
> Try this:
>
> c2 <- h
> years <- names(h)
> for (t in seq(years))
> {
> year <- years[t]
> c2[[year]] <- sapply(colnames(h[[year]]), function(var)
> c[[t]][h[[year]][ ,var] > 0, h[[year]][var, ] > 0])
> }
>
> By the way, it's great that you included code in your question.
> However, I encountered a couple of errors when running you code (see
> below).
>
> Also, it would be better to use a different name for your list "c",
> because c() is a function in R.
>
> Jean
>
>>
>> library(zoo)
>> DF1 = data.frame(read.table(textConnection(" B C D E F G
>> 8025 1995 0 4 1 2
>> 8025 1997 1 1 3 4
>> 8026 1995 0 7 0 0
>> 8026 1996 1 2 3 0
>> 8026 1997 1 2 3 1
>> 8026 1998 6 0 0 4
>> 8026 1999 3 7 0 3
>> 8027 1997 1 2 3 9
>> 8027 1998 1 2 3 1
>> 8027 1999 6 0 0 2
>> 8028 1999 3 7 0 0
>> 8029 1995 0 2 3 3
>> 8029 1998 1 2 3 2
>> 8029 1999 6 0 0 1"),head=TRUE,stringsAsFactors=FALSE))
>>
>> a <- read.zoo(DF1, split = 1, index = 2, FUN = identity)
>> sum.na <- function(x) if (any(!is.na(x))) sum(x, na.rm = TRUE) else NA
>> b <- rollapply(a, 3, sum.na, align = "right", partial = TRUE)
>
> Error in FUN(cdata[st, i], ...) : unused argument(s) (partial = TRUE)
>
> rollapply() has no argument partial.
rollapply was re-written for zoo 1.7.0 and the partial argument was
among the enhancements.
--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com
More information about the R-help
mailing list