neversaint
neversaint

Reputation: 64054

How to obtain string not longer than certain length in R

I have a vector of strings that looks like this

x <- c("WDNTO","WDRN","WDSAX","WECAX","WEN","WEDTO")

What I want to do is to extract only strings not longer than 4 characters, resulting in

  WDNT,WDRN,WDSA,WECA,WEN,WEDT

What's the way to do it?

Upvotes: 1

Views: 191

Answers (2)

generic_user
generic_user

Reputation: 3562

substr(x,1,4)
?substr

My first answer on SO!

Upvotes: 9

mnel
mnel

Reputation: 115435

Use substr, eg

substr(x, 1, 4)

Upvotes: 3

Related Questions