How to print a character of a certain length

The code below is a simplified example of my code, I want to only print/show the character from that variable that have a word length of over five characters:

z <- c("gday", "Hello", "Hi", "Hellokindsir")
z <- as.character(z)
# The only 'word' from the character values above that is longer than 5 letters is
# 'Hellokindsir', and thus is what I want to print/show. 

You can use the nchar() function to count characters.

z <- c("gday", "Hello", "Hi", "Hellokindsir")
print(z[nchar(z) > 5])
[1] "Hellokindsir"
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.