R base functions

subset

subset is a function that helps you take subsets of data. By default, subset removes NA rows.

subset does not perform any operation that can’t be accomplished by indexing.

Examples

Using the Craigslist vehicles file, read ALL of the rows of the data set into a new data frame, but only the three columns called state and long and lat (the other columns will not be needed). Create a subset of this new data frame, satisfying 3 conditions, namely, the state variable indicates that the data is from Indiana, and the long and lat values are not missing.

Click to see solution
myDF <- fread("/anvil/projects/tdm/data/craigslist/vehicles.csv",
              stringsAsFactors = TRUE, select = c(23:25))
subDF <- subset(myDF, (state=="in") & (!is.na(long)) & (!is.na(lat)))