[R] CONFUSSING WITH select[!miss] <- 1:sum(!miss)

Jim Lemon drjimlemon at gmail.com
Tue Dec 6 22:06:16 CET 2016


Hi Greg,
What is happening is easy to see:

 ph<-matrix(sample(1:100,40),ncol=4)
 colnames(ph)<-c("M1","X1","X2","X3")
 ph[sample(1:10,3),1]<-NA
 ph
       M1 X1 X2 X3
 [1,]  34 98  3 35
 [2,]  13 66 74 68
 [3,]  NA 22 99 79
 [4,]  94  6 80 36
 [5,]  18  9 16 65
 [6,]  NA 29 56 90
 [7,]  41 23  7 55
 [8,] 100 93 71 70
 [9,]  NA 61  8 57
[10,]  25  4 47 60
# get a logical vector showing which rows contain NA
 miss <- apply(is.na(ph[,c("M1","X1","X2","X3")]),1, any)
 miss
 [1] FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE
# create a vector of zeros the length of the number of rows
 select <- integer(nrow(ph))
 select
 [1] 0 0 0 0 0 0 0 0 0 0
# get the indices for the rows that do _not_ have NAs
 select[!miss] <- 1:sum(!miss)
 select
 [1] 1 2 0 3 4 0 5 6 0 7

If this is to select the rows without NAs, it may be easier to do:

which(!miss)
[1]  1  2  4  5  7  8 10

Jim



On Wed, Dec 7, 2016 at 5:18 AM, greg holly <mak.hholly at gmail.com> wrote:
> Dear All;
>
> I am very new in R and try to understand the logic for a program has been
> run sucessfully. Here select[!miss] <- 1:sum(!miss) par is confussing me. I
> need to understandand the logic behind this commend line.
>
> Thanks in advance for your help,
>
> Greg
>
>
> miss <- apply(is.na(ph[,c("M1","X1","X2","X3")]),1, any)
> select <- integer(nrow(ph))
> select[!miss] <- 1:sum(!miss)
>
>         [[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