简体   繁体   中英

R, searching specific size words in text, nchar function

I have a long text and I counted letters in every single word of it, now I want to show the shortest and the longest words. I used:

words<-strsplit(text," ") 
nchar(words[[1]])
w<-factor(nchar(words[[1]]))
table(w)

and I got a table with amount of words of specific length. And now, for example I know that the longest word has 19 letters, but how can I find and show that one word from whole text?

EDIT: and how to show for example every 5-letters word?

Try which.max to find longest word

words[[1]][which.max(nchar(words[[1]]))]

If you want to find all 5-letter words, try below

words[[1]][nchar(words[[1]])==5]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM