I would like to trim a character vector to the first five characters in each element. In this example I would like to trim each number in string to the first five characters. I am sure there must be an easy way to do this.
string<-
c("3243423",
"23423",
"34243234",
"2342",
"32544532",
"85678657")
I would like to have a a vector
c("32434",
"23423",
"34243",
"2342",
"32544",
"85678")
The following works as well (didn't know about strtrim
)
substring(string, 1, 5)
Figured it out. Hope this is helpful.
strtrim(string, 5)
stri_sub
function from stringi
package
stri_sub("123456789",1,4)
## [1] "1234"
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