I need to abbreviate department names by their first character, so that strDept="Department of Justice"
becomes strDeptAbbr = "DoJ".
How can I abbreviate a string using stringr
?
Thank you
With base R
, you can do:
abbreviate("Department of Justice", 1, named = FALSE)
[1] "DoJ"
You can use:
library(stringr)
x="Department of Justice"
new_list=strsplit(x, " ")
str_sub(as.list(new_list[[1]]),1,1)
The previous answer by @tmfmnk is much better in my opinion.
Edit:
As @Lyngbakr pointed out, the following code will yield the final result requested:
paste(str_sub(as.list(new_list[[1]]),1,1), collapse = "")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With