Skip to content
Back to formatted view

Raw Message

Message-ID: <48368B80-591B-4E43-AF78-5328161D842D@dcn.davis.ca.us>
Date: 2019-01-27T03:10:56Z
From: Jeff Newmiller
Subject: Create a new list from selected items in another list
In-Reply-To: <CA+ZkTxvm5p5_ex0AYLa64OgcAQKgxD4zvMtpPL9+cGxCPKVaSw@mail.gmail.com>

BBB <- lapply( CCC, function( v ) v[ 0<v ] )

On January 26, 2019 6:32:58 PM PST, Ek Esawi <esawiek at gmail.com> wrote:
>Hi All--
>
>I have a list which contain variables 0s at the end of each vector on
>the isit. I want to create a new list with only numbers > 0.
>It seems simple, but i tried several option, none of which worked.
>
>CCC <-  list(A=c(1,2,3,0,0,0,0), B=c(2,3,4,5,0,0,0,0,0,0))
>
>for (i in 1:length(CCC)) {
>  for(j in 1:length(CCC[[i]])){
>    if(CCC[[i]][j]>0)
>    BBB[[i]][j] <- CCC[[i]][j] #BBB <- AAA
>    }
>}
> desired output is
>$A
>[1] 1 2 3
>
>$B
>[1] 2 3 4 5
>
>Thanks-EK
>
>______________________________________________
>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.

-- 
Sent from my phone. Please excuse my brevity.