How to check whether a string is in lowercase or uppercase in R?


We can use str_detect function to check whether a single string or a vector of strings is in lowercase or uppercase. Along with str_detect function, we need to use either upper or lower to check whether the string is in lowercase or uppercase and the output will be returned in TRUE or FALSE form, if the string will be in lowercase and we pass lower with str_detect function then the output will be TRUE and vice-versa.

Example

 Live Demo

x1<-letters[1:26]
x1

Output

[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
[20] "t" "u" "v" "w" "x" "y" "z"

Example

str_detect(x1,"[[:upper:]]")

Output

[1]  FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[13] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[25] FALSE FALSE

Example

 Live Demo

x2<-LETTERS[1:26]
x2

Output

[1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S"
[20] "T" "U" "V" "W" "X" "Y" "Z"

Example

str_detect(x2,"[[:upper:]]")

Output

[1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[16] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE

Example

 Live Demo

x3<-"india"
x3

Output

[1] "india"

Example

str_detect(x3,"[[:upper:]]")

Output

[1] FALSE

Example

 Live Demo

x4<-c("abc","abcd","abcde","bacdef")
x4

Output

[1] "abc" "abcd" "abcde" "bacdef"

Example

str_detect(x4,"[[:upper:]]")

Output

[1] FALSE FALSE FALSE FALSE

Example

 Live Demo

x5<-c("AK", "AL", "AR", "AS", "AZ", "CA", "CO", "CT", "DC", "DE", "FL", "GA", "GU", "HI", "IA", "ID", "IL", "IN", "KS", "KY", "LA", "MA", "MD", "ME", "MI", "MN", "MO", "MP", "MS", "MT", "NC", "ND", "NE", "NH", "NJ", "NM", "NV", "NY", "OH", "OK", "OR", "PA", "PR", "RI", "SC", "SD", "TN", "TX", "UM", "UT", "VA", "VI", "VT", "WA", "WI", "WV", "WY")
x5

Output

[1] "AK" "AL" "AR" "AS" "AZ" "CA" "CO" "CT" "DC" "DE" "FL" "GA" "GU" "HI" "IA"
[16] "ID" "IL" "IN" "KS" "KY" "LA" "MA" "MD" "ME" "MI" "MN" "MO" "MP" "MS" "MT"
[31] "NC" "ND" "NE" "NH" "NJ" "NM" "NV" "NY" "OH" "OK" "OR" "PA" "PR" "RI" "SC"
[46] "SD" "TN" "TX" "UM" "UT" "VA" "VI" "VT" "WA" "WI" "WV" "WY"

Example

str_detect(x5,"[[:upper:]]")

Output

[1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[16] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[31] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[46] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE

Example

 Live Demo

x6<-c("alabama", "alaska", "american samoa", "arizona", "arkansas", "california", "colorado", "connecticut", "delaware", "district of columbia", "florida", "georgia", "guam", "hawaii", "idaho", "illinois", "indiana", "iowa", "kansas", "kentucky", "louisiana", "maine", "maryland", "massachusetts", "michigan", "minnesota", "minor outlying islands", "mississippi", "missouri", "montana", "nebraska", "nevada", "new hampshire", "new jersey", "new mexico", "new york", "north carolina", "north dakota", "northern mariana islands", "ohio", "oklahoma", "oregon", "pennsylvania", "puerto rico", "rhode island", "south carolina", "south dakota", "tennessee", "texas", "u.s. virgin islands", "utah", "vermont", "virginia", "washington", "west virginia", "wisconsin", "wyoming")
x6

Output

[1] "alabama" "alaska"
[3] "american samoa" "arizona"
[5] "arkansas" "california"
[7] "colorado" "connecticut"
[9] "delaware" "district of columbia"
[11] "florida" "georgia"
[13] "guam" "hawaii"
[15] "idaho" "illinois"
[17] "indiana" "iowa"
[19] "kansas" "kentucky"
[21] "louisiana" "maine"
[23] "maryland" "massachusetts"
[25] "michigan" "minnesota"
[27] "minor outlying islands" "mississippi"
[29] "missouri" "montana"
[31] "nebraska" "nevada"
[33] "new hampshire" "new jersey"
[35] "new mexico" "new york"
[37] "north carolina" "north dakota"
[39] "northern mariana islands" "ohio"
[41] "oklahoma" "oregon"
[43] "pennsylvania" "puerto rico"
[45] "rhode island" "south carolina"
[47] "south dakota" "tennessee"
[49] "texas" "u.s. virgin islands"
[51] "utah" "vermont"
[53] "virginia" "washington"
[55] "west virginia" "wisconsin"
[57] "wyoming"

Example

str_detect(x6,"[[:upper:]]")

Output

[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[13] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[25] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[37] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[49] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE

Updated on: 16-Oct-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements