Get Started

String length

Our next stringr function is str_length(). str_length() takes a vector of strings as input and returns the number of characters in each string. For example, try finding the number of characters in Batman's name:

str_length(c("Bruce", "Wayne"))

This is very similar to the base function nchar() but you'll see in the exercises str_length() handles factors in an intuitive way, whereas nchar() will just return an error.

Historically, nchar() was even worse, rather than returning an error if you passed it a factor, it would return the number of characters in the numeric encoding of the factor. Thankfully this behavior has been fixed, but it was one of the original motivations behind str_length().

Take your first look at babynames by asking if girls' names are longer than boys' names.

This is a part of the course

“String Manipulation with stringr in R”

View Course

Exercise instructions

We've pulled out just the names from 2014, and created the vectors boy_names and girl_names for you. (If you want to learn about the filter() function, take the Data Manipulation in R with dplyr course!).

  • Take a look at the boy_names vector, it's long, so use head() to see the first few elements.
  • Use str_length() on boy_names to find the length of each name and save the result to boy_length.
  • Take a look at the lengths. Again, use head(). Can you see the correspondence with boy_names?
  • Find the length of all the girls' names. Call this girl_length.
  • Find the difference in mean length between boys' and girls' names by subtracting the mean length of boys' names from that of girls' names.
  • Confirm str_length() works on factors, by calling it on factor(boy_names). Again, you'll want to just look at the head().

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

library(stringr)
library(babynames)
library(dplyr)

# Extracting vectors for boys' and girls' names
babynames_2014 <- filter(babynames, year == 2014)
boy_names <- filter(babynames_2014, sex == "M")$name
girl_names <- filter(babynames_2014, sex == "F")$name

# Take a look at a few boy_names
___

# Find the length of all boy_names
boy_length <- ___

# Take a look at a few lengths
___

# Find the length of all girl_names
girl_length <- ___

# Find the difference in mean length
___

# Confirm str_length() works with factors
head(___)

This exercise is part of the course

String Manipulation with stringr in R

IntermediateSkill Level
4.4+
9 reviews

Learn how to pull character strings apart, put them back together and use the stringr package.

Time to meet stringr! You'll start by learning about some stringr functions that are very similar to some base R functions, then how to detect specific patterns in strings, how to split strings apart and how to find and replace parts of strings.

Exercise 1: Introducing stringrExercise 2: Putting strings together with stringrExercise 3: String length
Exercise 4: Extracting substringsExercise 5: Hunting for matchesExercise 6: Detecting matchesExercise 7: Subsetting strings based on matchExercise 8: Counting matchesExercise 9: Splitting stringsExercise 10: Parsing strings into variablesExercise 11: Some simple text statisticsExercise 12: Replacing matches in stringsExercise 13: Replacing to tidy stringsExercise 14: ReviewExercise 15: Final challenges

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free