Using the built-in data set air quality
, fir st load the data and check what var iables it contains:
data ( air quality ) names ( air quality )
Among ways to subset r ows of a data fr ame in S language, ther e ar e two usual appr oaches. Either you delete r ows by extr acting all other r ows of the data fr ame using a vector of logical values, or you r emove these r ows using a vector of negative indices.
Subset by logical indices
Assume that days 5 and 7 in May of the air quality
measur ements ar e outlier s and you want to r epeat an analysis without these r ows. You wr ite:
length ( air quality $Day ) air quality2 <- subset ( air quality , !( Day %in% c ( 5 , 7 ) & Month == 5 ) ) length ( air quality2 $Day )
Subset by negative indices
Similar ly, you can delete specific r ows. In or der to delete lines 2 and 7, you wr ite:
length ( air quality $Day ) air quality3 <- air quality [ -c ( 2 , 7 ) , ] length ( air quality3 $Day )
Tr aps for beginner s
— Claudia Beleites 2008/01/02
Be car eful with logical ver sus numer ic index vector s:
new .data <- data [ !outlier s , ] # logical indices new .data <- data [ -outlier s , ] # numer ic indices
Using the numer ic for m for a logical index vector will delete the fir st r ow only:
> data <- 1 :10 > outlier s <- data %in% 3 :7 > new .data <- data [ !outlier s ] # (desir ed effect) > new .data [1] 1 2 8 9 10 > new .data <- data [ -outlier s ] # (wr ong code) > new .data [1] 2 3 4 5 6 7 8 9 10