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]]
Hello, The first command line produces a logical vector with TRUE if at least one row element of ph is NA and FALSE otherwise. The second creates a vector of zeros with length equal to nrow(ph). Now the third command line. ! negates miss, so TRUE becomes FALSE and vice-versa. sum(!miss) counts how many not misses are there and 1:sum(!miss) creates a vector 1, 2, ..., sum(!miss). To see this print each of these components one by one: print(miss) print(!miss) print(sum(!miss)) etc And select[!miss] uses a logical index into 'select' to set only the values of 'select' where !miss is TRUE equal to 1, 2, 3, ... I believe you should read R-intro.pdf that comes with your installation of R more carefully, specially sections 2.4 and 2.7. Hope this helps, Rui Barradas Em 06-12-2016 18:18, greg holly escreveu:> 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. >
R is interactive so you can print the intermediate results:> ph <- data.frame(M1=c(1,NA,3,4,5), X1=1:5, X2=c(1,2,NA,4,5), X3=1:5,Y=c(11,12,13,14,NA), row.names=paste0("R",1:5))> phM1 X1 X2 X3 Y R1 1 1 1 1 11 R2 NA 2 2 2 12 R3 3 3 NA 3 13 R4 4 4 4 4 14 R5 5 5 5 5 NA> miss <- apply(is.na(ph[,c("M1","X1","X2","X3")]),1, any) > missR1 R2 R3 R4 R5 FALSE TRUE TRUE FALSE FALSE> select <- integer(nrow(ph)) > select[1] 0 0 0 0 0> sum(!miss)[1] 3> select[!miss] <- 1:sum(!miss) > select[1] 1 0 0 2 3 Then you can look in the Introduction to R document or ask about the steps that confuse you. Bill Dunlap TIBCO Software wdunlap tibco.com On Tue, Dec 6, 2016 at 10: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. >[[alternative HTML version deleted]]
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.
Hi Jim, Rui and William; I do appreciate for your explanations and help. These are very helpful. Regards, Hayrettin On Tue, Dec 6, 2016 at 4:06 PM, Jim Lemon <drjimlemon at gmail.com> wrote:> 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. >[[alternative HTML version deleted]]