0
0
R Programmingprogramming~15 mins

nchar and substring in R Programming - Mini Project: Build & Apply

Choose your learning style9 modes available
Using nchar and substring in R
📖 Scenario: You work in a library and have a list of book titles. You want to find out how long each title is and extract a part of each title to create short labels.
🎯 Goal: Build a small R program that calculates the length of each book title using nchar and extracts the first 5 characters of each title using substring.
📋 What You'll Learn
Create a vector called book_titles with exactly these titles: "Data Science Basics", "R Programming", "Statistics 101"
Create a variable called title_lengths that stores the length of each title using nchar
Create a variable called short_labels that stores the first 5 characters of each title using substring
Print the title_lengths and short_labels variables
💡 Why This Matters
🌍 Real World
Libraries, bookstores, or any place with lists of names or titles often need to measure string lengths and create short labels for display.
💼 Career
Knowing how to manipulate strings is essential for data cleaning, reporting, and user interface design in many programming and data analysis jobs.
Progress0 / 4 steps
1
Create the book titles vector
Create a vector called book_titles with these exact strings: "Data Science Basics", "R Programming", "Statistics 101"
R Programming
Need a hint?

Use the c() function to create a vector with the exact titles.

2
Calculate the length of each title
Create a variable called title_lengths that stores the length of each title in book_titles using the nchar function
R Programming
Need a hint?

Use nchar(book_titles) to get the length of each string in the vector.

3
Extract the first 5 characters of each title
Create a variable called short_labels that stores the first 5 characters of each title in book_titles using the substring function
R Programming
Need a hint?

Use substring(book_titles, 1, 5) to get the first 5 characters of each string.

4
Print the results
Print the variables title_lengths and short_labels to display the lengths and short labels of the book titles
R Programming
Need a hint?

Use print(title_lengths) and print(short_labels) to show the results.