[R] If NA Problem!
Peter Ehlers
ehlers at ucalgary.ca
Fri Sep 2 17:21:35 CEST 2011
On 2011-09-02 05:16, Anna Dunietz wrote:
> Hi All!
>
> Please find code and the respective lists below. My problem: I specify the
> case that lilwin[[p]] is not an NA and want the code found in iwish to be
> returned ONLY for that case. Why do I get a list of length 2 (and why is
> NULL the first element)? I understand that the code below is quite
> senseless. I have run into a problem while working on a large project and
> wanted to simplify it in order for it to be more understandable and
> accessible. If I should not be using the if function, please let me know
> what I should be doing instead. I know that I must use the for function for
> my project. The thing I most want to understand is how, after specifying a
> certain condition, one may save certain data that occurs when that condition
> is met. I hope I have been clear enough!
>
> Thank you very much for your help!
> Anna
>
> biglist<-list(a=1:4,b=2:6)
> lilwin<-list(x=NA,y=2)
> lilloss<-list(m=1,n=3)
>
>
>
>> biglist$a
> [1] 1 2 3 4
>
> $b
> [1] 2 3 4 5 6
>> lilwin$x
> [1] NA
>
> $y
> [1] 2
>> lilloss$m
> [1] 1
>
> $n
> [1] 3
>
>
> iwish<-list()
> for(p in 1:length(biglist)){
> if(is.na(lilwin[[p]])==F) iwish[p]<-list(biglist[[p]][lilwin[[p]]])
> }
>
>
>> iwish[[1]]
> NULL
>
> [[2]]
> [1] 3
Jean has given you one fix. Here's another (see ?'c'):
iwish<-list()
for(p in seq_along(biglist)){
if(!is.na(lilwin[[p]]))
iwish <- c(iwish, biglist[[p]][lilwin[[p]]])
}
BTW, it's not a good idea to use 'F' instead of FALSE and the
negation operator is usually a better way to test.
Peter Ehlers
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
More information about the R-help
mailing list