MulaiMulai sekarang secara gratis

Replacing to tidy strings

You've seen one common strategy to pull variables out of strings is to split the string based on a pattern. Sometimes, it's easier to just replace the parts you don't want with an empty string "". This is also a common strategy to clean strings up, for example, to remove unwanted punctuation or white space.

In this exercise you'll pull out some numbers by replacing the part of the string that isn't a number, you'll also play with the format of some phone numbers. Pay close attention to the difference between str_replace() and str_replace_all().

Latihan ini adalah bagian dari kursus

String Manipulation with stringr in R

Lihat Kursus

Latihan interaktif praktis

Cobalah latihan ini dengan menyelesaikan kode contoh berikut.

# Some IDs
ids <- c("ID#: 192", "ID#: 118", "ID#: 001")

# Replace "ID#: " with ""
id_nums <- ___

# Turn id_nums into numbers
id_ints <- ___
Edit dan Jalankan Kode