I am trying to rename values in my data all at once, rather than doing it entry by entry.
Right now I am using this code to perform my changes:
INV$Classification[INV$Classification=="lay net"] <- "Illegal Lay Net"
My data is INV, and my column is Classification. I want to find all instances of the words "lay net" and replace them with "Illegal Lay Net". I am doing this manually right now, which I could ultimately do in excel. Is there a more efficient way to do this?
For Example I have entries such as: POSSIBLE ILLEGAL LAY NET or Lay Nets Violation, these do not get picked up by just using the "lay net" find and thus are not switched because this code is very specific.
You want to start using regular expressions. Take a look at gsub
, grep
, grepl
and similar functions. Of course it is not a magic bullet, you still have to understand and make sure you catch every case.
Here is an example that will catch the 3 cases you said:
INV$Classification[grepl("lay net", INV$Classification, ignore.case=T)] <- "Illegal Lay Net"