How to Subset a Data Frame in R (4 Examples)

You can use the following basic syntax to subset a data frame in R:

`df[rows, columns]`

The following examples show how to use this syntax in practice with the following data frame:

```#create data frame
df <- data.frame(team=c('A', 'A', 'B', 'B', 'C', 'C', 'C'),
points=c(77, 81, 89, 83, 99, 92, 97),
assists=c(19, 22, 29, 15, 32, 39, 14))

#view data frame
df

team points assists
1    A     77      19
2    A     81      22
3    B     89      29
4    B     83      15
5    C     99      32
6    C     92      39
7    C     97      14
```

Example 1: Subset Data Frame by Selecting Columns

The following code shows how to subset a data frame by column names:

```#select all rows for columns 'team' and 'assists'
df[ , c('team', 'assists')]

team assists
1    A      19
2    A      22
3    B      29
4    B      15
5    C      32
6    C      39
7    C      14
```

We can also subset a data frame by column index values:

```#select all rows for columns 1 and 3
df[ , c(1, 3)]

team assists
1    A      19
2    A      22
3    B      29
4    B      15
5    C      32
6    C      39
7    C      14```

Example 2: Subset Data Frame by Excluding Columns

The following code shows how to subset a data frame by excluding specific column names:

```#define columns to exclude
cols <- names(df) %in% c('points')

#exclude points column
df[!cols]

team assists
1    A      19
2    A      22
3    B      29
4    B      15
5    C      32
6    C      39
7    C      14
```

We can also exclude columns using index values

```#exclude column 2
df[ , c(-2)]

team assists
1    A      19
2    A      22
3    B      29
4    B      15
5    C      32
6    C      39
7    C      14```

Example 3: Subset Data Frame by Selecting Rows

The following code shows how to subset a data frame by specific rows:

```#select rows 1, 5, and 7
df[c(1, 5, 7), ]

team points assists
1    A     77      19
5    C     99      32
7    C     97      14
```

We can also subset a data frame by selecting a range of rows:

```#select rows 1 through 5
df[1:5, ]

team points assists
1    A     77      19
2    A     81      22
3    B     89      29
4    B     83      15
5    C     99      32```

Example 4: Subset Data Frame Based on Conditions

The following code shows how to use the subset() function to select rows and columns that meet certain conditions:

```#select rows where points is greater than 90
subset(df, points > 90)

team points assists
5    C     99      32
6    C     92      39
7    C     97      14
```

We can also use the | (“or”) operator to select rows that meet one of several conditions:

```#select rows where points is greater than 90 or less than 80
subset(df, points > 90 | points < 80)

team points assists
1    A     77      19
5    C     99      32
6    C     92      39
7    C     97      14```

We can also use the & (“and”) operator to select rows that meet multiple conditions:

```#select rows where points is greater than 90 and assists is greater than 30
subset(df, points > 90 & assists > 30)

team points assists
5    C     99      32
6    C     92      39```

We can also use the select argument to only select certain columns based on a condition:

```#select rows where points is greater than 90 and only show 'team' column
subset(df, points > 90, select=c('team'))

team
5    C
6    C
7    C```