0

I have data that looks like this:

                  sources
 1:           Jana’s iPhone
 2:     Richard's iPhone 6
 3:               Denise's
 4:           Sara’s iPhone
 5:     Jeff’s Apple Watch
 6:    BLAIR’s Apple Watch
 7:      Sunshine's iPhone
 8:         Brian's iPhone
 9: Jonathan’s Apple Watch
10: patricia’s Apple Watch

I'm trying to replace any string that contains iPhone just say iPhone. How can I do this?

I tried the following on a data table sources that contains a column also called sources:

sources[length(grep("iPhone", sources)) > 0, sources:= "iPhone"]

But this converts all rows to "iPhone" even if the row did not originally contain a string with "iPhone". I am guessing this is because grep or length is not vectorized, so I end up selecting all rows. So then my question becomes how can I identify the rows that contain a substring?

0

1 Answer 1

5

Use grepl instead:

sources[grepl("iPhone", sources), sources:= "iPhone"]
Sign up to request clarification or add additional context in comments.

1 Comment

And fixed = TRUE for efficiency

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.