Skip to main content

4.4) Removing Unwanted Elements


You can use a minus sign in the square brackets to remove whole rows or columns – e.g. GRASS <- GRASS[-6,] to remove row 6.

Find three ways to remove the column GRASS$tlrs.1.

We now have two ways to extract more than one column of a dataframe at once: either remove the ones you don't want, or specify the ones you do want using single square brackets. Use both methods to get the 1st, 3rd and 4th columns of GRASS displayed on the screen. Why was this not possible using the method for lists?

Some functions don’t run if they are given NAs, or they simply evaluate to NA. We can normally get round this by setting an "NA remove" argument to TRUE: na.rm=TRUE. Sometimes, however, we may want manually to remove unwanted rows, perhaps to ensure exactly the same data are used in a series of analyses. We can use the function is.na(), which searches for NAs and returns a logical object of the same shape as its input, containing TRUE for each element that is NA and FALSE for all the others. Now, to select the rows of a data frame that are not NAs for a particular variable, just add the 'not' symbol ! to this: e.g. GRASS <- GRASS[!is.na(GRASS$Dms), ].