Hi, I am working on a data set which looks like this:> head(temp)Day Month Year PW ROW 1 1 1 1959 NA 6.40 2 2 1 1959 6.65 6.35 3 3 1 1959 2.50 3.60 4 4 1 1959 0.60 2.25 5 5 1 1959 0.85 0.30 6 6 1 1959 0.00 2.20 I am trying to extract all the rows containing NA. I can extrat all the rows without NAs in column 4 using> PWna<-temp[complete.cases(temp[,4]),] > head(PWna)Day Month Year PW ROW 2 2 1 1959 6.65 6.35 3 3 1 1959 2.50 3.60 4 4 1 1959 0.60 2.25 5 5 1 1959 0.85 0.30 6 6 1 1959 0.00 2.20 7 7 1 1959 1.40 1.65 But can't figure out how to extract the rows WITH the NAs. Can anyone advise me? Thanks in advance -- View this message in context: http://r.789695.n4.nabble.com/Extracting-dataframe-rows-containing-NAs-in-one-column-tp3937361p3937361.html Sent from the R help mailing list archive at Nabble.com.
Pete Brecknock
2011-Oct-25 17:26 UTC
[R] Extracting dataframe rows containing NAs in one column
kaallen wrote:> > Hi, > > I am working on a data set which looks like this: > >> head(temp) > Day Month Year PW ROW > 1 1 1 1959 NA 6.40 > 2 2 1 1959 6.65 6.35 > 3 3 1 1959 2.50 3.60 > 4 4 1 1959 0.60 2.25 > 5 5 1 1959 0.85 0.30 > 6 6 1 1959 0.00 2.20 > > I am trying to extract all the rows containing NA. I can extrat all the > rows without NAs in column 4 using > >> PWna<-temp[complete.cases(temp[,4]),] >> head(PWna) > Day Month Year PW ROW > 2 2 1 1959 6.65 6.35 > 3 3 1 1959 2.50 3.60 > 4 4 1 1959 0.60 2.25 > 5 5 1 1959 0.85 0.30 > 6 6 1 1959 0.00 2.20 > 7 7 1 1959 1.40 1.65 > > But can't figure out how to extract the rows WITH the NAs. Can anyone > advise me? > > Thanks in advance >I am sure others will have cleaner approaches but how about .... # Data temp = data.frame(Day=1:6,Month=1, Year=c(NA,1959,NA,1959,1959,1959), PW=c(6,7,8,NA,10,11)) # Extract Any Row Containing an NA myNAs = temp[apply(temp,1,function(x) any(is.na(x))),] HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/Extracting-dataframe-rows-containing-NAs-in-one-column-tp3937361p3937523.html Sent from the R help mailing list archive at Nabble.com.
R. Michael Weylandt
2011-Oct-25 17:48 UTC
[R] Extracting dataframe rows containing NAs in one column
I'm thinking you need to spend some time reading about subsetting in R: temp[!complete.cases(temp),] # I don't think you need/want/should use the [,4] Michael On Tue, Oct 25, 2011 at 12:38 PM, kaallen <kaallen at liv.ac.uk> wrote:> Hi, > > I am working on a data set which looks like this: > >> head(temp) > ?Day Month Year ? PW ?ROW > 1 ? 1 ? ? 1 1959 ? NA 6.40 > 2 ? 2 ? ? 1 1959 6.65 6.35 > 3 ? 3 ? ? 1 1959 2.50 3.60 > 4 ? 4 ? ? 1 1959 0.60 2.25 > 5 ? 5 ? ? 1 1959 0.85 0.30 > 6 ? 6 ? ? 1 1959 0.00 2.20 > > I am trying to extract all the rows containing NA. I can extrat all the rows > without NAs in column 4 using > >> PWna<-temp[complete.cases(temp[,4]),] >> head(PWna) > ?Day Month Year ? PW ?ROW > 2 ? 2 ? ? 1 1959 6.65 6.35 > 3 ? 3 ? ? 1 1959 2.50 3.60 > 4 ? 4 ? ? 1 1959 0.60 2.25 > 5 ? 5 ? ? 1 1959 0.85 0.30 > 6 ? 6 ? ? 1 1959 0.00 2.20 > 7 ? 7 ? ? 1 1959 1.40 1.65 > > But can't figure out how to extract the rows WITH the NAs. Can anyone advise > me? > > Thanks in advance > > -- > View this message in context: http://r.789695.n4.nabble.com/Extracting-dataframe-rows-containing-NAs-in-one-column-tp3937361p3937361.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >