How to find maximum string length by column in data frame

18,183

We can use nchar

max(nchar(clmn))

For finding the maximum character length for each column

lapply(df1, function(x) max(nchar(x)))

If we need to filter the columns that have maximum string length greater than 1

df1[sapply(df1, function(x) max(nchar(x)))>1]

Or

Filter(function(x) max(nchar(x)) >1, df1)
Share:
18,183
Kalin Stoyanov
Author by

Kalin Stoyanov

Updated on June 16, 2022

Comments

  • Kalin Stoyanov
    Kalin Stoyanov almost 2 years

    My question is similar to this. But for strings.

    So I have a dataframe, each column contains strings of different length. So, how I can find the maximum string length per column?

    Then, how to select the columns, where length is > 1, by sapply or similar.

    A typical column of the dataframe looks like this:

    clmn=c("XDX", "GUV", "FQ", "ACUE", "HIT", "AYX", "NFD", "AHBW", "GKQ", "PYF")
    

    Thanks