Subset Data Frame Observations in R

Your goal

You need to subset a data frame's observations in R.

Step-by-step tutorial

Apply indices to subset a data frame's observations.

Select a specific range of rows

> precip.df <- read.csv('precip-central-park.csv')
> head(precip.df)
  YEAR  JAN  FEB  MAR  APR  MAY  JUN  JUL  AUG  SEP  OCT  NOV  DEC ANNUAL
1 1869 2.53 6.87 4.61 1.39 4.15 4.40 3.20 1.76 2.81 6.48 2.03 5.02  45.25
2 1870 4.41 2.83 3.33 5.11 1.83 2.82 3.76 3.07 2.52 4.97 2.42 2.18  39.25
3 1871 2.07 2.72 5.54 3.03 4.04 7.05 5.57 5.60 2.34 7.50 3.56 2.24  51.26
4 1872 1.88 1.29 3.74 2.29 2.68 2.93 7.83 6.29 2.95 3.35 4.08 3.18  42.49
5 1873 5.34 3.80 2.09 4.16 3.69 1.28 4.61 9.56 3.14 2.73 4.63 2.96  47.99
6 1874 5.33 2.04 2.12 8.77 2.24 2.78 5.06 2.43 8.24 1.70 2.30 2.82  45.83
> precip.df[1:4,]
  YEAR  JAN  FEB  MAR  APR  MAY  JUN  JUL  AUG  SEP  OCT  NOV  DEC ANNUAL
1 1869 2.53 6.87 4.61 1.39 4.15 4.40 3.20 1.76 2.81 6.48 2.03 5.02  45.25
2 1870 4.41 2.83 3.33 5.11 1.83 2.82 3.76 3.07 2.52 4.97 2.42 2.18  39.25
3 1871 2.07 2.72 5.54 3.03 4.04 7.05 5.57 5.60 2.34 7.50 3.56 2.24  51.26
4 1872 1.88 1.29 3.74 2.29 2.68 2.93 7.83 6.29 2.95 3.35 4.08 3.18  42.49

Select all rows starting from a given index

To select all rows starting from index n, it's often easier just to tell R to skip the first n - 1 rows. Here we'll select all rows starting from index 146:

> nrow(precip.df)
[1] 151
> precip.df[-(1:145),]
    YEAR  JAN  FEB  MAR  APR  MAY  JUN  JUL  AUG  SEP  OCT  NOV  DEC ANNUAL
146 2014 2.79 5.48 3.67 7.85 4.37 4.26 5.59 2.25 1.21 5.77 4.51 6.04  53.79
147 2015 5.23 2.04 4.72 2.08 1.86 4.79 3.98 2.35 3.28 3.91 2.01 4.72  40.97
148 2016 4.41 4.40 1.17 1.61 3.75 2.60 7.02 1.97 2.79 4.15 5.41 2.89  42.17
149 2017 4.83 2.48 5.25 3.84 6.38 4.76 4.19 3.34 2.00 4.18 1.58 2.21  45.04
150 2018 2.18 5.83 5.17 5.78 3.53 3.11 7.45 8.59 6.19 3.59 7.62 6.51  65.55
151 2019 3.58 3.14 3.87 4.55 6.82 5.46 5.77 3.70 0.95 6.15 1.95 7.09  53.03