R trimws on a character vector
Trim every badge name while preserving registration order.
A simple example
trimws on a character vector
- Start with " A ", " B "
- Do this Trim each element
- Result "A", "B"
Syntax
trimws(names)
Run these examples in R
Save one example at a time as Lesson.R. Use R 4.x.
Rscript --vanilla Lesson.RClean the names on two visitor badges
A reception desk receives visitor names copied from an email. Extra spaces at the edges would make the printed badges look uneven, so the desk cleans each name while preserving arrival order.
From the programcat(paste(trimws(badges), collapse = ", "))
ResultMira, Sam
badges <- c(" Mira ", " Sam")
cat(paste(trimws(badges), collapse = ", "))
Mira, Sam
The cleaned names are ready for the badge printer.
Advertisements
Clean a volunteer roster
Two names pasted from a form have stray spaces. The printer trims surrounding spaces from each label. Which names remain on the badge sheet?
Given“ Isa” · “Lou ”
Worked resultIsa · Lou
Prepare two exhibit labels
A curator copies padded names out of a spreadsheet. The printer trims surrounding spaces from each label. Which names remain on the badge sheet?
Given“ Clay ” · “ Ink”
Worked resultClay · Ink
What to watch for
trimws returns cleaned strings. Retain the result for printing.