[R] a vector unusually getting NA values.....
Duncan Murdoch
murdoch.duncan at gmail.com
Sat Jan 20 13:14:29 CET 2018
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]]
>
> ______________________________________________
> 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.
>
More information about the R-help
mailing list