a vector unusually getting NA values.....

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

a vector unusually getting NA values.....

akshay kulkarni
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]

        return(snlf1)

}


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):

        k

[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,

AKSHAY M KULKARNI

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: a vector unusually getting NA values.....

Duncan Murdoch-2
On 20/01/2018 6:53 AM, akshay kulkarni wrote:

> 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]
>
>          return(snlf1)
>
> }
>
>
> 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):

Try this:

k <- 1
k[5] <- 5
k

and you should see what's happening.

Duncan Murdoch

>
>          k
>
> [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,
>
> AKSHAY M KULKARNI
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.