[R] a vector unusually getting NA values.....

akshay kulkarni akshay_e4 at hotmail.com
Sat Jan 20 12:53:50 CET 2018

dear members,

                             I have a function by name "ygcudf" and a list of stock names by name"snl" ( of class "list"). ygcudf acts on snl and returns a list of the most favourable  stocks. I don't wish to divulge the code of the function, for genuine reasons, but the algorithm is as follows:


        k <- 1; i <- 1

       for(j in 1:length(snl)){

                                 if(snl[[j]] == "condition") {

                                       k[i] <- j

                                        i <- i+1}


        snlf1 <- snl[k]



The problem is, if we test the function by returning the vector k, it, (k), is getting NA values. i.e, instead of skipping the values of j for which the condition is false, k is getting populated by NA values. k is getting the following values (for a given snl):


[1]    1 NA NA NA 5 6 NA NA 9 10 11 NA NA NA NA 16

If j is initialised to the values for which k is NA, and the function is  made to return j, it is returning the relevant j. I don't know how the k is getting NA values. Will the following change work:

      snlf1 <-  snl[na.omit(k)]

What is wrong ??

Sorry for not parting with the code...please bear with me....

your help will be highly appreciated

yours sincerely,


	[[alternative HTML version deleted]]

More information about the R-help mailing list