amrita
amrita

Reputation: 133

select rows in R with some value

I have a dataset like:

df
id1                             id2
Liquor - Alcohol/Illegal sale    23
Arson                            21
Alcohol/Sale without license     20
Burglary                         34

Is there some way to replace all Alcohol related rows (here, rows 1 and 3) with just a single string "Alcohol"? For eg:

df
id1                             id2
Alcohol                          23
Arson                            21
Alcohol                          20
Burglary                         34

Thanks!

Upvotes: 0

Views: 27

Answers (1)

Michele Usuelli
Michele Usuelli

Reputation: 2000

You can use grep.

rows_with_alcohol <- grep("Alcohol", df$id1)
df[rows_with_alcohol, "id1"] <- "Alcohol"

Upvotes: 1

Related Questions