Home > OS >  R Delete all rows in dataframe based on index
R Delete all rows in dataframe based on index

Time:10-12

I have a list of indices that I know I want to remove from my data frame.

Normally I can do this easily with just writing out the names but I don't understand why the following command works when I want to keep the rows I am deleting:

str(data)
'data.frame':   180 obs. of  624 variables:
$ Sites                                              : chr  "SS0501_1" "SS0570_1" "SS0609_1" "SS0645_1" ...
$ LandUse                                            : chr  "Urban" "Urban" "Urban" "Urban" ...
.
.
.

f_pattern <- "SS2371|SS1973|SS1908|SS1815|SS1385|SS1304" # find index names in data frame using partial site names
get_full_id <- data[grep(f_pattern, rownames(data)),] # get the full site names (these are indices in the data frame)

data <- data[!get_full_id$Sites,] # DOES NOT WORK
Error in !check$Sites : invalid argument type

However, it does work if I pull these sites out.

data <- data[get_full_id$Sites,] # Works fine, I get a dataframe with 6 rows...the ones I don't want to keep.
str(data)
'data.frame':   6 obs. of  624 variables:
$ Sites                                              : chr  "SS1908_1" "SS1973_1" "SS1304_2" "SS1385_2" ...
$ LandUse                                            : chr  "Urban" "Rural" "Rural" "Urban" ...
.
.

I don't understand why the reverse with "!" won't work at all?

CodePudding user response:

If the dataset have rownames, then we may need - instead of ! (if it is an exact match (- not clear as the rownames are not showed))

data[-get_full_id$Sites,]

because the negation works on a logical vector. Here, we are asking to return the rows that doesn't match the rownames in 'Sites' column. If we want to use !, create a logical vector

data[!row.names(data) %in% get_full_id$Sites,]

This also works only if there is an exact match


Also, this can be done directly

data[-grep(f_pattern, rownames(data)),]

Or use invert = TRUE

data[grep(f_pattern, rownames(data), invert = TRUE),]
  •  Tags:  
  • r
  • Related