-2

I have a data.table "temp"

library(data.table)
library(stringr) 

temp <- data.table(name=c("test1","test2","test3","test4"), 
                   country=c("usa","usa","mexico","mexico"))

I want to apply function to column "name" where country is "usa" in data.table way.

For example apply function str_replace() from stringr:

str_replace( temp[country == "usa"]$name ,'\\w*[a-z]', '')

any advice or links are welcome!

Shin
  • 251
  • 1
  • 3
  • 8
  • 2
    Please take your time to read [these](https://github.com/Rdatatable/data.table/wiki/Getting-started). Also, prior posting questions on the R tag, please consult the examples [here](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). If this wasn't for akrun, we would probably close this question as not reproducible. – David Arenburg Dec 28 '16 at 07:33

1 Answers1

0

We can use

library(data.table)
setDT(temp)[country=="usa", name := str_replace(name, "\\w*[a-z]", "")]
akrun
  • 874,273
  • 37
  • 540
  • 662