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 [[alternative HTML version deleted]]
Anna Dunietz wrote on 09/02/2011 07:16:45 AM:> > Hi All! > > Please find code and the respective lists below. My problem: I specifythe> case that lilwin[[p]] is not an NA and want the code found in iwish tobe> returned ONLY for that case. Why do I get a list of length 2 (and whyis> NULL the first element)?The list is length 2 because when p=2 in your for loop you save the output to iwish[2]. Since nothing was saved to iwish[1] (when lilwin[[1]] was NA) that gives a NULL value for iwish[1]. If you want to allow the length of iwish to be shorter, you need to change the way you save your output. For example, iwish <- list() count <- 0 for(p in 1:length(biglist)){ if(is.na(lilwin[[p]])==F) { count <- count + 1 iwish[count] <- list(biglist[[p]][lilwin[[p]]]) } } Jean> I understand that the code below is quite > senseless. I have run into a problem while working on a large projectand> 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 meknow> what I should be doing instead. I know that I must use the for functionfor> my project. The thing I most want to understand is how, afterspecifying a> certain condition, one may save certain data that occurs when thatcondition> 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[[alternative HTML version deleted]]
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] 3Jean 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.