The post is about the R Language Reference Guide subsetting Vectors, Lists, Matrices, and Data Frames in R Language.

R language Reference Guide is about learning R Programming with a short description of the widely used commands. It will help the learner and intermediate user of the R Programming Language to get help with different functions quickly. This R language reference is classified into different groups. Let us start with the R Language Reference Guide – III.

## Table of Contents

This R Language Quick Reference contains R commands about subsetting in R, such as subsetting of vectors, matrices, lists, data frames, arrays, and factors. It also discusses setting the different properties related to R language data types.

### Subsetting Vectors: Quick R Language Reference

The following are ways to subset or slice the values from a vector.

R Command | Short Description |
---|---|

x[1:5] | Select elements of $x$ by index |

x[-(1:5)] | Exclude elements of $x$ by index |

x[c(TRUE, FALSE)] | Select elements of $x$ corresponding to the True value |

x[c(“a”, “b”)] | Select elements of $x$ by name |

### Subsetting Lists in R Language

The following methods are used to subset or slice a list in R Language.

R Command | Short Description |
---|---|

x[1:5] | Extracts a sublist of the list $x$ |

x[-(1:5)] | Extract a sublist by excluding elements of list $x$ |

x[c(TRUE, FALSE)] | Extract a sublist with logical subscripts |

x[c(“a”, “b”)] | Extract a sublist by name |

x[[2]] | Extract an element of the list $x$ |

x[[“a”]] | Extract the element with the name “a” from list $x$ |

x$a | Extract the element with the name “a” from list $x$ |

### Subsetting Matrices in R: A Quick Reference

To subset or extract certain elements from a matrix follow the ways described below.

R Command | Short Description |
---|---|

x[i, j] | Extracts elements of matrix $x$, specified by row $i$ and column $j$ |

x[i, j] = v | Set or rest the elements of matrix $x$, specified by row $i$ and column $j$ |

x[i, ] | Extracts $i$th row of a matrix $x$ |

x[i, ] = v | Set or resets the $i$th row of a matrix $x$ specified by $i$th row |

x[ , j] | Extracts the $j$ column of a matrix $x$ |

x[ , j] = v | Sets or resets the $j$ column of matrix $x$ |

x[i] | Subets a matrix $x$ as a vector |

x[i] = v | Sets or resets the $i$th elements (treated as a vector operation) |

### Subsetting a Data Frame in R Language

One can easily subset or slice a Data Frame in R.

R Command | Short Description |
---|---|

df[i, j] | Matrix subsetting of a data frame, specified by $i$th row and $j$th column |

df[i, j] = df | Sets or resets a subset of a data frame |

subset(df, subset = i) | Subset of the $i$ cases/ observations of a data frame |

subset(df, select = i) | Subset of the $i$ variables/ columns of a data frame |

subset(df, subset=i, select=j) | Subset of the $i$ cases and $j$ variables of a data frame |