Hello, I would appreciate if someone could help me resolve the following: 1. df1[!is.na( X1 | X2 | X3 | X4 | X5),][,1:5] # This does not work 2. Is these message harmful? The following object(s) are masked from 'df1 (position 3)': X1, X2, X3, X4, X5 Thanks, Pradip Muhuri #Reproducible Example set.seed(5) df1<-data.frame(matrix(sample(c(1:10,NA),100,replace=TRUE),ncol=5)) attach (df1) #delete rows if any of them NA for X1 df1[!is.na( X1),][,1:5] # This works #delete rows if any of them NA for X1, X2, X3, X4 or X5 df1[!is.na( X1 | X2 | X3 | X4 | X5),][,1:5] # This does not work
Hi, is.na( X1 | X2 | X3 | X4 | X5) isn't a valid construct. You'd need !(is.na(X1) | is.na(X2) etc ) Or more elegantly df1[apply(df1, 1, function(x)all(!is.na(x))), ] Sarah On Thursday, November 22, 2012, Muhuri, Pradip (SAMHSA/CBHSQ) wrote:> Hello, > > I would appreciate if someone could help me resolve the following: > > 1. df1[!is.na( X1 | X2 | X3 | X4 | X5),][,1:5] # This does not work > > 2. Is these message harmful? The following object(s) are masked from 'df1 > (position 3)': > X1, X2, X3, X4, X5 > > Thanks, > > Pradip Muhuri > > > #Reproducible Example > set.seed(5) > df1<-data.frame(matrix(sample(c(1:10,NA),100,replace=TRUE),ncol=5)) > attach (df1) > #delete rows if any of them NA for X1 > df1[!is.na( X1),][,1:5] # This works > > #delete rows if any of them NA for X1, X2, X3, X4 or X5 > df1[!is.na( X1 | X2 | X3 | X4 | X5),][,1:5] # This does not work > > ______________________________________________ > R-help@r-project.org <javascript:;> mailing list > 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. >-- Sarah Goslee http://www.stringpage.com http://www.sarahgoslee.com http://www.functionaldiversity.org [[alternative HTML version deleted]]
Hi do you want this?> df1[complete.cases(df1),]X1 X2 X3 X4 X5 2 8 8 3 2 10 6 8 6 7 10 1 11 4 5 5 10 8 12 6 1 7 8 4 17 5 7 3 1 3 18 10 7 3 8 7 19 7 5 3 5 6 20 10 5 2 4 6 Regards Petr> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Muhuri, Pradip (SAMHSA/CBHSQ) > Sent: Thursday, November 22, 2012 3:11 PM > To: Muhuri, Pradip (SAMHSA/CBHSQ); r-help at r-project.org > Subject: [R] Data Extraction > > Hello, > > I would appreciate if someone could help me resolve the following: > > 1. df1[!is.na( X1 | X2 | X3 | X4 | X5),][,1:5] # This does not work > > 2. Is these message harmful? The following object(s) are masked from > 'df1 (position 3)': > X1, X2, X3, X4, X5 > > Thanks, > > Pradip Muhuri > > > #Reproducible Example > set.seed(5) > df1<-data.frame(matrix(sample(c(1:10,NA),100,replace=TRUE),ncol=5)) > attach (df1) > #delete rows if any of them NA for X1 > df1[!is.na( X1),][,1:5] # This works > > #delete rows if any of them NA for X1, X2, X3, X4 or X5 df1[!is.na( X1 > | X2 | X3 | X4 | X5),][,1:5] # This does not work > > ______________________________________________ > R-help at r-project.org mailing list > 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.
On 22-11-2012, at 15:11, Muhuri, Pradip (SAMHSA/CBHSQ) wrote:> Hello, > > I would appreciate if someone could help me resolve the following: > > 1. df1[!is.na( X1 | X2 | X3 | X4 | X5),][,1:5] # This does not work > > 2. Is these message harmful? The following object(s) are masked from 'df1 (position 3)': > X1, X2, X3, X4, X5 > > Thanks, > > Pradip Muhuri > > > #Reproducible Example > set.seed(5) > df1<-data.frame(matrix(sample(c(1:10,NA),100,replace=TRUE),ncol=5)) > attach (df1) > #delete rows if any of them NA for X1 > df1[!is.na( X1),][,1:5] # This works > > #delete rows if any of them NA for X1, X2, X3, X4 or X5 > df1[!is.na( X1 | X2 | X3 | X4 | X5),][,1:5] # This does not workYet another way of doing this is df1[!is.na(rowSums(df1)),][1:5] But Petr's solution appears to be quickest. See this:> N <- 100000 > set.seed(13) > df <- data.frame(matrix(sample(c(1:10,NA),N,replace=TRUE),ncol=50)) > library(rbenchmark) > > f1 <- function(df) {df[apply(df, 1, function(x)all(!is.na(x))),][,1:ncol(df)]} > f2 <- function(df) {df[!is.na(rowSums(df)),][1:ncol(df)]} > f3 <- function(df) {df[complete.cases(df),][1:ncol(df)]} > > benchmark(d1 <- f1(df), d2 <- f2(df), d3 <- f3(df), columns=c("test","elapsed", "relative", "replications"))test elapsed relative replications 1 d1 <- f1(df) 3.675 13.172 100 2 d2 <- f2(df) 0.401 1.437 100 3 d3 <- f3(df) 0.279 1.000 100> identical(d1,d2)[1] TRUE> identical(d1,d3)[1] TRUE Berend
Hi Sarah, I am glad you have precisely caught where I made the mistake. Thank you so much. regards, Pradip Muhuri ________________________________________ From: Sarah Goslee [sarah.goslee at gmail.com] Sent: Thursday, November 22, 2012 9:21 AM To: Muhuri, Pradip (SAMHSA/CBHSQ) Cc: r-help at r-project.org Subject: Re: [R] Data Extraction Hi, is.na<http://is.na/>( X1 | X2 | X3 | X4 | X5) isn't a valid construct. You'd need !(is.na<http://is.na>(X1) | is.na<http://is.na>(X2) etc ) Or more elegantly df1[apply(df1, 1, function(x)all(!is.na<http://is.na>(x))), ] Sarah On Thursday, November 22, 2012, Muhuri, Pradip (SAMHSA/CBHSQ) wrote: Hello, I would appreciate if someone could help me resolve the following: 1. df1[!is.na<http://is.na>( X1 | X2 | X3 | X4 | X5),][,1:5] # This does not work 2. Is these message harmful? The following object(s) are masked from 'df1 (position 3)': X1, X2, X3, X4, X5 Thanks, Pradip Muhuri #Reproducible Example set.seed(5) df1<-data.frame(matrix(sample(c(1:10,NA),100,replace=TRUE),ncol=5)) attach (df1) #delete rows if any of them NA for X1 df1[!is.na<http://is.na>( X1),][,1:5] # This works #delete rows if any of them NA for X1, X2, X3, X4 or X5 df1[!is.na<http://is.na>( X1 | X2 | X3 | X4 | X5),][,1:5] # This does not work ______________________________________________ R-help at r-project.org<javascript:;> mailing list 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. -- Sarah Goslee http://www.stringpage.com http://www.sarahgoslee.com http://www.functionaldiversity.org