R Minimum & Maximum Number of Characters in Column Strings (2 Examples)

In this R tutorial you’ll learn how to return the number of characters in a string in a data frame variable.

Creation of Example Data

my_df <- data.frame(x1 = c("xx", "xxxx", "xxx"),  # Example data frame in R
                   x2 = c("y", "yyyy", "yyyyyyy"),
                   x3 = c("zzzz", "zz", "zzzz"))
my_df                                             # Showing example data in RStudio console
#     x1      x2   x3
# 1   xx       y zzzz
# 2 xxxx    yyyy   zz
# 3  xxx yyyyyyy zzzz

Example 1: Find Minimum Number of Characters in String in Data Frame Columns

apply(my_df,                                      # Minimum length
      2,
      function(x) min(nchar(x)))

Example 2: Find Maximum Number of Characters in String in Data Frame Columns

apply(my_df,                                      # Maximum length
      2,
      function(x) max(nchar(x)))

Related Tutorials & Further Resources

Please find some further R tutorials below:

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top