-2

I have a data.frame like this:

Client Product   
1      VV_Brazil_Jul
2      VV_Brazil_Mar
5      VV_US_Jul
1      VV_JP_Apr
3      VV_CH_May
6      VV_Brazil_Aug

I would like to delete all rows with "Brazil".

rashikawa
  • 41
  • 3

2 Answers2

1

You can do this using the grepl function and the ! to find the cases that are not matched:

# Create a dataframe where some cases have the product with Brazil as part of the value
df <- structure(list(Client = c(1L, 2L, 5L, 1L, 3L, 6L), 
                     Product = c("VV_Brazil_Jul", "VV_Brazil_Mar", "VV_US_Jul", "VV_JP_Apr", "VV_CH_May", "VV_Brazil_Aug")), 
                row.names = c(NA, -6L), class = c("data.table", "data.frame"))

# Display the original dataframe in the Console
df

# Limit the dataframe to cases which do not have Brazil as part of the product
df <- df[!grepl("Brazil", df$Product, ignore.case = TRUE),]

# Display the revised dataframe in the Console
df
Kerry Jackson
  • 1,821
  • 12
  • 20
  • It didn't work. It didn't give any error but I am still not able to see the data frame without Brazil. I was wondering if I could use .substract? – rashikawa Aug 03 '18 at 18:43
  • Sorry, I forgot a comma before when subsetting using the square brackets. Try the revised command. – Kerry Jackson Aug 03 '18 at 18:54
0

You can do the same thing with the tidyverse collection

dplyr::slice(df, -stringr::str_which(df$Product, "Brazil"))