시작하기무료로 시작하기

Pulling out parts of a phone number

You can now go back to the phone number example from the previous chapter. You developed a pattern to extract the parts of string that looked like phone numbers, and now you have the skills to pull out the pieces of the number. Let's see if you can put your skills together to output the first phone number in each string in a common format.

이 연습은 강의의 일부입니다

String Manipulation with stringr in R

강의 보기

연습 안내

We've put the pieces of your pattern in your workspace, along with some text containing phone numbers in the variable contact.

  • phone_pattern is the pattern you developed in the last chapter. Edit it to capture() each sequence of numbers.
  • Use str_match() to grab all the pieces into phone_numbers.
  • Put together the pieces with str_c() into the format (XXX) XXX-XXXX.

실습형 인터랙티브 연습

이 예제를 이 샘플 코드를 완성하여 풀어보세요.

# View text containing phone numbers
contact

# Add capture() to get digit parts
phone_pattern <- three_digits %R% zero_or_more(separator) %R% 
           three_digits %R% zero_or_more(separator) %R%
           four_digits
           
# Pull out the parts with str_match()
phone_numbers <- ___

# Put them back together
___(
  ___,
  ___,
  ___,
  ___,
  ___,
  ___)
코드 편집 및 실행