Challenge: Compose Your Own Functions
Swipe to start coding
Create two simple functions and compose them to clean a list of strings.
- The
trim_whitespacefunction must remove leading and trailing whitespace from each string in the input vector. - The
to_lowercasefunction must convert all characters in each string of the input vector to lowercase. - The
clean_stringsfunction must use bothtrim_whitespaceandto_lowercaseto process the input vector and return the cleaned result.
Lösung
The gsub("^\\s+|\\s+$", "", x) command uses regular expressions to clean up strings:
^\\s+matches one or more whitespace characters at the start of a string;|means 'or';\\s+$matches one or more whitespace characters at the end of a string.
gsub replaces all matches of these patterns with an empty string (""), effectively removing all leading and trailing spaces from each element in the vector x. This leaves only the inner content of each string untouched.
Danke für Ihr Feedback!
single
Fragen Sie AI
Fragen Sie AI
Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen
Can you explain this in simpler terms?
What are the main points I should remember?
Can you give me an example?
Großartig!
Completion Rate verbessert auf 5.56
Challenge: Compose Your Own Functions
Swipe um das Menü anzuzeigen
Swipe to start coding
Create two simple functions and compose them to clean a list of strings.
- The
trim_whitespacefunction must remove leading and trailing whitespace from each string in the input vector. - The
to_lowercasefunction must convert all characters in each string of the input vector to lowercase. - The
clean_stringsfunction must use bothtrim_whitespaceandto_lowercaseto process the input vector and return the cleaned result.
Lösung
The gsub("^\\s+|\\s+$", "", x) command uses regular expressions to clean up strings:
^\\s+matches one or more whitespace characters at the start of a string;|means 'or';\\s+$matches one or more whitespace characters at the end of a string.
gsub replaces all matches of these patterns with an empty string (""), effectively removing all leading and trailing spaces from each element in the vector x. This leaves only the inner content of each string untouched.
Danke für Ihr Feedback!
single