how to subset everything after first row containing a numeric value

Viewed 176

Subsetting question. I would prefer to use built in R functions but doesn't have to be. I believe solution is simple, but Im new to R.

Here is example data:

df <- data.frame(year = c("2001", "2002", "2003", "2004", "2005", "2006"),
              C1 = c("a", "b", "c", "d", "e", "f"), 
              C2 = c(NA, NA, 35, 20, NA, 50),
              C3=1:6)

Output looks like this:

  year C1 C2 C3
1 2001  a NA  1
2 2002  b NA  2
3 2003  c 35  3
4 2004  d 20  4
5 2005  e NA  5
6 2006  f 50  6

I want to select all columns starting at the first row with a numeric value (ie, >0) in column C2, so that my output would look like this:

  year C1 C2 C3
1 2003  c 35  3
2 2004  d 20  4
3 2005  e NA  5
4 2006  f 50  6

Notice the NA in column C2 row 3 is not excluded (which is desired). I've tried the following, but it excludes NA rows:

new_df=subset(df, C2>0)

Ive also tried this but doesn't work either:

new_df=subset(df, C2>0 | is.na(C2))
3 Answers
Related