[R] How to remove second row if above row contain <NA>?

Kristi Glover kristi.glover at hotmail.com
Mon Mar 21 05:50:26 CET 2016


Hi R user, 
Is there any easiest way to delete the second row if the first row contains NA?
Here is the example. A file "dat" contains 13 rows, in which there are 2 NAs. one "NA" is in row 1 and another in row 10. Therefore, I want to delete row 2 and row 11 (just want to delete a row of underneath of the row contains "NA"). I have table with almost 15000 rows. The attached table is just an example.
Your help is highly appreciated. 

I want to dat1 from dat

dat<-structure(list(s = c(1L, 2L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 15L, 16L), Species.x = structure(c(NA, 1L, 1L, 2L, 2L, 2L, 
3L, 3L, 4L, NA, 5L, 5L, 5L), .Label = c("Sp1", "Sp2", "Sp3", "Sp4", "Sp5"), class = "factor"), sign.x = structure(c(NA, 1L, 
1L, 2L, 1L, 1L, 2L, 1L, 2L, NA, 1L, 1L, 1L), .Label = c("", "marked"), class = "factor"), observedSite.x = structure(c(NA, 3L, 1L, 
2L, 4L, 3L, 1L, 2L, 2L, NA, 5L, 1L, 3L), .Label = c("SiteA", "SiteB", "SiteC", "SiteD", "SIteD"), class = "factor"), Time.x = structure(c(NA, 
4L, 5L, 2L, 3L, 10L, 7L, 9L, 1L, NA, 2L, 6L, 8L), .Label = c("1/15/15", "1/17/15", "2/18/15", "2/25/15", "2/27/15", "2/28/15", "3/27/15", 
"3/5/15", "4/19/15", "7/3/15"), class = "factor"), Month.x = structure(c(NA, 1L, 2L, 1L, 2L, 3L, 3L, 4L, 1L, NA, 1L, 2L, 3L), .Label = c("T1", 
"T2", "T3", "T4"), class = "factor"), Species.y = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 4L, 5L, 5L, 5L, 5L), .Label = c("Sp1", 
"Sp2", "Sp3", "Sp4", "Sp5"), class = "factor"), sign.y = structure(c(2L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 1L), .Label = c("", 
"marked"), class = "factor"), observedSite.y = structure(c(1L, 3L, 1L, 2L, 4L, 3L, 1L, 2L, 2L, 3L, 5L, 1L, 3L), .Label = c("SiteA", 
"SiteB", "SiteC", "SiteD", "SIteD"), class = "factor"), Time.y = structure(c(1L, 4L, 5L, 2L, 3L, 10L, 7L, 9L, 1L, 1L, 2L, 6L, 8L), .Label = c("1/15/15", 
"1/17/15", "2/18/15", "2/25/15", "2/27/15", "2/28/15", "3/27/15", "3/5/15", "4/19/15", "7/3/15"), class = "factor"), Month.y = structure(c(1L, 
1L, 2L, 1L, 2L, 3L, 3L, 4L, 1L, 1L, 1L, 2L, 3L), .Label = c("T1", "T2", "T3", "T4"), class = "factor")), .Names = c("s", "Species.x", "sign.x", "observedSite.x", "Time.x", "Month.x", "Species.y", "sign.y", "observedSite.y", "Time.y", "Month.y"), class = "data.frame", row.names = c(NA, -13L))

dat1<-structure(list(X = c(1L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 12L, 13L), s = c(1L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 15L, 16L), 
    Species.x = structure(c(NA, 1L, 2L, 2L, 2L, 3L, 3L, 4L, NA,  5L, 5L), .Label = c("Sp1", "Sp2", "Sp3", "Sp4", "Sp5"), class = "factor"), 
    sign.x = structure(c(NA, 1L, 2L, 1L, 1L, 2L, 1L, 2L, NA, 1L, 1L), .Label = c("", "marked"), class = "factor"), observedSite.x = structure(c(NA, 
    1L, 2L, 4L, 3L, 1L, 2L, 2L, NA, 1L, 3L), .Label = c("SiteA", "SiteB", "SiteC", "SiteD"), class = "factor"), Time.x = structure(c(NA, 
    4L, 2L, 3L, 9L, 6L, 8L, 1L, NA, 5L, 7L), .Label = c("1/15/15", "1/17/15", "2/18/15", "2/27/15", "2/28/15", "3/27/15", "3/5/15", 
    "4/19/15", "7/3/15"), class = "factor"), Month.x = structure(c(NA, 2L, 1L, 2L, 3L, 3L, 4L, 1L, NA, 2L, 3L), .Label = c("T1", 
    "T2", "T3", "T4"), class = "factor"), Species.y = structure(c(1L, 1L, 2L, 2L, 2L, 3L, 3L, 4L, 5L, 5L, 5L), .Label = c("Sp1", 
    "Sp2", "Sp3", "Sp4", "Sp5"), class = "factor"), sign.y = structure(c(2L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 1L), .Label = c("", "marked"
    ), class = "factor"), observedSite.y = structure(c(1L, 1L, 2L, 4L, 3L, 1L, 2L, 2L, 3L, 1L, 3L), .Label = c("SiteA", 
    "SiteB", "SiteC", "SiteD"), class = "factor"), Time.y = structure(c(1L, 4L, 2L, 3L, 9L, 6L, 8L, 1L, 1L, 5L, 7L), .Label = c("1/15/15", 
    "1/17/15", "2/18/15", "2/27/15", "2/28/15", "3/27/15", "3/5/15", "4/19/15", "7/3/15"), class = "factor"), Month.y = structure(c(1L, 
    2L, 1L, 2L, 3L, 3L, 4L, 1L, 1L, 2L, 3L), .Label = c("T1", "T2", "T3", "T4"), class = "factor")), .Names = c("X", "s", 
"Species.x", "sign.x", "observedSite.x", "Time.x", "Month.x", "Species.y", "sign.y", "observedSite.y", "Time.y", "Month.y"), class = "data.frame", row.names = c(NA, -11L))


More information about the R-help mailing list