How many characters in each column name of dataframe ?

Hi,
I would like to know how many characters are in each and every variable name of dataframe.
Basically I want to use something like nchar for all columns at once.
How do I do it ?

sapply(colnames(mtcars),nchar)
#>  mpg  cyl disp   hp drat   wt qsec   vs   am gear carb 
#>    3    3    4    2    4    2    4    2    2    4    4

Created on 2023-01-06 with reprex v2.0.2

1 Like

Thank you very much indeed, exactly what I wanted.

1 Like

Would it be possible to do it in some tidyverse way as well ?

Yes.

# done on tablet and not  checked
new_frame <- data.frame(var_names = colnames(mtcars)) %>%
     mutate(name_length = nchar(var_names))

The trade off is perhaps more intuitive syntax than {base}, but more of it.

It works like a charm, even on tablet you are The Boss, thank you very, very much Sir.

1 Like

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.