Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Data Visualization
  • Statistics in R
  • Machine Learning in R
  • Data Science in R
  • Packages in R
  • Data Types
  • String
  • Array
  • Vector
  • Lists
  • Matrices
  • Oops in R
Open In App
Next Article:
How to Loop Through Column Names in R dataframes?
Next article icon

How to find maximum string length by column in R DataFrame ?

Last Updated : 21 Nov, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we are going to see how to find maximum string length by column in R Programming Language. 

To find the maximum string length by column in the given dataframe, first, nchar() function is called to get the length of all the string present in the particular column of the dataframe, and then the max() function must be called to get the maximum value of the length of the string generated by the nchar() function. The nchar() and the max() function are base functions of the R programming language, so there is no need to import any package.

nchar() function takes a character vector as an argument and returns a vector whose elements contain the sizes of the corresponding elements of x

Syntax:

nchar(x, type = "chars", allowNA = FALSE, keepNA = NA)

Parameter:

  • x: character vector or a vector to be coerced to a character vector. Giving a factor is an error.
  • type:character string: partial matching to one of c("bytes", "chars", "width"). See ‘Details’.
  • allowNA: logical, should NA be returned for invalid multibyte strings or "bytes"-encoded strings (rather than throwing an error)?
  • keepNA:logical: should NA be returned wherever x is NA?

max() function finds the maximum value among the data provided.

Syntax:

MAX(vector, rank = 1, value = FALSE, rank.adjust = TRUE, forceChoice = FALSE)

Parameter:

  • vector: Vector in which maximum/minimum element needs to be identified
  • rank:value(s) or rank(s) of maximum values.
  • value:  Should value or rank be returned?
  • rank.adjust: If the maximum value of a range of ranks exceeds vector length, should this be adjusted?
  • forceChoice: In the case of ties, should all results be returned or only one?

Example1:

R
gfg_data <- data.frame(x = c("geeks", "for", "geeks"),                          y = c("I", "Love", "Coding"),                        z=c("R", "programming ", "language"))  max(nchar(gfg_data$y))   

Output:

[1] 6

Example 2:

R
gfg_data <- data.frame(x = c("geeks", "for", "geeks"),                          y = c("I", "Love", "Coding"),                        z=c("R", "programming ", "language"))  max(nchar(gfg_data$z))   

Output:

[1] 12

Next Article
How to Loop Through Column Names in R dataframes?
author
geetansh044
Improve
Article Tags :
  • R Language
  • R-DataFrame
  • R DataFrame-Programs

Similar Reads

  • How to Loop Through Column Names in R dataframes?
    In this article, we will discuss how to loop through column names in dataframe in R Programming Language. Method 1: Using sapply() Here we are using sapply() function with some functions to get column names. This function will return column names with some results Syntax: sapply(dataframe,specific f
    2 min read
  • How to Get substring from a column in PySpark Dataframe ?
    In this article, we are going to see how to get the substring from the PySpark Dataframe column and how to create the new column and put the substring in that newly created column. We can get the substring of the column using substring() and substr() function. Syntax: substring(str,pos,len) df.col_n
    3 min read
  • How to get name of dataframe column in PySpark ?
    In this article, we will discuss how to get the name of the Dataframe column in PySpark.  To get the name of the columns present in the Dataframe we are using the columns function through this function we will get the list of all the column names present in the Dataframe. Syntax: df.columns We can a
    3 min read
  • Get Column Index in Data Frame by Variable Name in R
    R is an open-source programming language that is used as a statistical software and data analysis tool. In R Programming Language we can work on specific columns based on their names. In this article, we will learn different methods to extract the Get Column Index in Data Frame by Variable Name in R
    5 min read
  • How to Combine Two Columns into One in R dataframe?
    In this article, we will discuss how to combine two columns into one in dataframe in R Programming Language.  Method 1 : Using paste() function This function is used to join the two columns in the dataframe with a separator. Syntax: paste(data$column1, data$column2, sep=" ") where data is the input
    2 min read
  • How to Find the Longest or Shortest Text String in a Column in Excel?
    In this article, we will see how to find the longest or shortest text string in a column in Excel? Usually, for finding the longest or shortest string we can visit the all string in columns one by one and compare them to get results. This seems to work when you have less amount of data in an excel s
    4 min read
  • How to find length of matrix in R
    In this article, we will examine various methods to find the length of a matrix by using R Programming Language. What is a matrix?A matrix is a two-dimensional data structure that is a collection of rows and columns. A matrix can able to contain data of various types such as numeric, characters, and
    4 min read
  • How to check for a substring in a PySpark dataframe ?
    In this article, we are going to see how to check for a substring in PySpark dataframe. Substring is a continuous sequence of characters within a larger string size. For example, "learning pyspark" is a substring of "I am learning pyspark from GeeksForGeeks". Let us look at different ways in which w
    5 min read
  • How to find length of data frame in R
    In this article, we will see What is a Data Frame and how to find the length of a data frame in R programming Language. Return the length (total number of rows) of the Data Frame using nrow()nrow() function is used to return the number of rows of the specified object (Matrix/DataFrame etc). we will
    3 min read
  • How to show full column content in a PySpark Dataframe ?
    Sometimes in Dataframe, when column data containing the long content or large sentence, then PySpark SQL shows the dataframe in compressed form means the first few words of the sentence are shown and others are followed by dots that refers that some more data is available. From the above sample Data
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences