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 ncharCreate a variable called
short_labels that stores the first 5 characters of each title using substringPrint 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