How to abbreviate a string in R
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 = "")
With base R
, you can do:
abbreviate("Department of Justice", 1, named = FALSE)
[1] "DoJ"