How to truncate character vector with three dots after n characters in R?


To truncate character vector with three dots after n characters can be done with the help of str_trunc function of stringr package. For example, if we have a character vector say x and each value containing 10 characters then truncating those values with three dots after 5 characters can be done by using the command str_trunc(x,8).

Example1

Live Demo

> x1<-sample(c("abcbefgh","ijklmnopq","rstuvwxyz"),100,replace=TRUE)
> x1

Output

[1] "rstuvwxyz" "rstuvwxyz" "abcbefgh" "rstuvwxyz" "ijklmnopq" "ijklmnopq"
[7] "ijklmnopq" "rstuvwxyz" "rstuvwxyz" "rstuvwxyz" "rstuvwxyz" "abcbefgh"
[13] "rstuvwxyz" "abcbefgh" "abcbefgh" "ijklmnopq" "ijklmnopq" "ijklmnopq"
[19] "ijklmnopq" "rstuvwxyz" "rstuvwxyz" "abcbefgh" "abcbefgh" "ijklmnopq"
[25] "ijklmnopq" "ijklmnopq" "rstuvwxyz" "rstuvwxyz" "rstuvwxyz" "rstuvwxyz"
[31] "rstuvwxyz" "abcbefgh" "abcbefgh" "rstuvwxyz" "rstuvwxyz" "abcbefgh"
[37] "ijklmnopq" "abcbefgh" "rstuvwxyz" "ijklmnopq" "abcbefgh" "ijklmnopq"
[43] "rstuvwxyz" "ijklmnopq" "abcbefgh" "rstuvwxyz" "abcbefgh" "abcbefgh"
[49] "rstuvwxyz" "ijklmnopq" "rstuvwxyz" "ijklmnopq" "ijklmnopq" "ijklmnopq"
[55] "rstuvwxyz" "ijklmnopq" "abcbefgh" "rstuvwxyz" "ijklmnopq" "ijklmnopq"
[61] "abcbefgh" "abcbefgh" "rstuvwxyz" "abcbefgh" "ijklmnopq" "ijklmnopq"
[67] "rstuvwxyz" "abcbefgh" "abcbefgh" "abcbefgh" "ijklmnopq" "abcbefgh"
[73] "rstuvwxyz" "rstuvwxyz" "ijklmnopq" "ijklmnopq" "ijklmnopq" "ijklmnopq"
[79] "ijklmnopq" "rstuvwxyz" "rstuvwxyz" "abcbefgh" "abcbefgh" "ijklmnopq"
[85] "ijklmnopq" "abcbefgh" "abcbefgh" "rstuvwxyz" "abcbefgh" "abcbefgh"
[91] "rstuvwxyz" "rstuvwxyz" "ijklmnopq" "rstuvwxyz" "ijklmnopq" "ijklmnopq"
[97] "abcbefgh" "abcbefgh" "ijklmnopq" "ijklmnopq"

Example

> library(stringr)
> str_trunc(x1,5)

Output

[1] "rs..." "rs..." "ab..." "rs..." "ij..." "ij..." "ij..." "rs..." "rs..."
[10] "rs..." "rs..." "ab..." "rs..." "ab..." "ab..." "ij..." "ij..." "ij..."
[19] "ij..." "rs..." "rs..." "ab..." "ab..." "ij..." "ij..." "ij..." "rs..."
[28] "rs..." "rs..." "rs..." "rs..." "ab..." "ab..." "rs..." "rs..." "ab..."
[37] "ij..." "ab..." "rs..." "ij..." "ab..." "ij..." "rs..." "ij..." "ab..."
[46] "rs..." "ab..." "ab..." "rs..." "ij..." "rs..." "ij..." "ij..." "ij..."
[55] "rs..." "ij..." "ab..." "rs..." "ij..." "ij..." "ab..." "ab..." "rs..."
[64] "ab..." "ij..." "ij..." "rs..." "ab..." "ab..." "ab..." "ij..." "ab..."
[73] "rs..." "rs..." "ij..." "ij..." "ij..." "ij..." "ij..." "rs..." "rs..."
[82] "ab..." "ab..." "ij..." "ij..." "ab..." "ab..." "rs..." "ab..." "ab..."
[91] "rs..." "rs..." "ij..." "rs..." "ij..." "ij..." "ab..." "ab..." "ij..."
[100] "ij..."

Example

> str_trunc(x1,7)

Output

[1] "rstu..." "rstu..." "abcb..." "rstu..." "ijkl..." "ijkl..." "ijkl..."
[8] "rstu..." "rstu..." "rstu..." "rstu..." "abcb..." "rstu..." "abcb..."
[15] "abcb..." "ijkl..." "ijkl..." "ijkl..." "ijkl..." "rstu..." "rstu..."
[22] "abcb..." "abcb..." "ijkl..." "ijkl..." "ijkl..." "rstu..." "rstu..."
[29] "rstu..." "rstu..." "rstu..." "abcb..." "abcb..." "rstu..." "rstu..."
[36] "abcb..." "ijkl..." "abcb..." "rstu..." "ijkl..." "abcb..." "ijkl..."
[43] "rstu..." "ijkl..." "abcb..." "rstu..." "abcb..." "abcb..." "rstu..."
[50] "ijkl..." "rstu..." "ijkl..." "ijkl..." "ijkl..." "rstu..." "ijkl..."
[57] "abcb..." "rstu..." "ijkl..." "ijkl..." "abcb..." "abcb..." "rstu..."
[64] "abcb..." "ijkl..." "ijkl..." "rstu..." "abcb..." "abcb..." "abcb..."
[71] "ijkl..." "abcb..." "rstu..." "rstu..." "ijkl..." "ijkl..." "ijkl..."
[78] "ijkl..." "ijkl..." "rstu..." "rstu..." "abcb..." "abcb..." "ijkl..."
[85] "ijkl..." "abcb..." "abcb..." "rstu..." "abcb..." "abcb..." "rstu..."
[92] "rstu..." "ijkl..." "rstu..." "ijkl..." "ijkl..." "abcb..." "abcb..."
[99] "ijkl..." "ijkl..."

Example

> str_trunc(x1,8)

Output

[1] "rstuv..." "rstuv..." "abcbefgh" "rstuv..." "ijklm..." "ijklm..."
[7] "ijklm..." "rstuv..." "rstuv..." "rstuv..." "rstuv..." "abcbefgh"
[13] "rstuv..." "abcbefgh" "abcbefgh" "ijklm..." "ijklm..." "ijklm..."
[19] "ijklm..." "rstuv..." "rstuv..." "abcbefgh" "abcbefgh" "ijklm..."
[25] "ijklm..." "ijklm..." "rstuv..." "rstuv..." "rstuv..." "rstuv..."
[31] "rstuv..." "abcbefgh" "abcbefgh" "rstuv..." "rstuv..." "abcbefgh"
[37] "ijklm..." "abcbefgh" "rstuv..." "ijklm..." "abcbefgh" "ijklm..."
[43] "rstuv..." "ijklm..." "abcbefgh" "rstuv..." "abcbefgh" "abcbefgh"
[49] "rstuv..." "ijklm..." "rstuv..." "ijklm..." "ijklm..." "ijklm..."
[55] "rstuv..." "ijklm..." "abcbefgh" "rstuv..." "ijklm..." "ijklm..."
[61] "abcbefgh" "abcbefgh" "rstuv..." "abcbefgh" "ijklm..." "ijklm..."
[67] "rstuv..." "abcbefgh" "abcbefgh" "abcbefgh" "ijklm..." "abcbefgh"
[73] "rstuv..." "rstuv..." "ijklm..." "ijklm..." "ijklm..." "ijklm..."
[79] "ijklm..." "rstuv..." "rstuv..." "abcbefgh" "abcbefgh" "ijklm..."
[85] "ijklm..." "abcbefgh" "abcbefgh" "rstuv..." "abcbefgh" "abcbefgh"
[91] "rstuv..." "rstuv..." "ijklm..." "rstuv..." "ijklm..." "ijklm..."
[97] "abcbefgh" "abcbefgh" "ijklm..." "ijklm..."

Example2

Live Demo

> x2<-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")
> x2

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_trunc(x2,5)

Output

[1] "Al..." "Al..." "Am..." "Ar..." "Ar..." "Ca..." "Co..." "Co..." "De..."
[10] "Di..." "Fl..." "Ge..." "Guam" "Ha..." "Idaho" "Il..." "In..." "Iowa"
[19] "Ka..." "Ke..." "Lo..." "Maine" "Ma..." "Ma..." "Mi..." "Mi..." "Mi..."
[28] "Mi..." "Mi..." "Mo..." "Ne..." "Ne..." "Ne..." "Ne..." "Ne..." "Ne..."
[37] "No..." "No..." "No..." "Ohio" "Ok..." "Or..." "Pe..." "Pu..." "Rh..."
[46] "So..." "So..." "Te..." "Texas" "U...." "Utah" "Ve..." "Vi..." "Wa..."
[55] "We..." "Wi..." "Wy..."

Example

> str_trunc(x2,4)

Output

[1] "A..." "A..." "A..." "A..." "A..." "C..." "C..." "C..." "D..." "D..."
[11] "F..." "G..." "Guam" "H..." "I..." "I..." "I..." "Iowa" "K..." "K..."
[21] "L..." "M..." "M..." "M..." "M..." "M..." "M..." "M..." "M..." "M..."
[31] "N..." "N..." "N..." "N..." "N..." "N..." "N..." "N..." "N..." "Ohio"
[41] "O..." "O..." "P..." "P..." "R..." "S..." "S..." "T..." "T..." "U..."
[51] "Utah" "V..." "V..." "W..." "W..." "W..." "W..."

Updated on: 04-Mar-2021

125 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements