เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

Playing with URLs

In the last exercise, we've extracted the raw number of tweets which contain the pattern "github". So far, this is just a raw number: 347, and you're still not able to determine which proportion of the total number of URLs this 347 represents.

In this exercise, we'll build a function called str_prop_detected(), which takes a character vector and a pattern, and returns the proportion of elements which contain this pattern. This function could be used on other datasets to detect the proportion of pattern matches in a character vector.

Here, we'll combine some purrr functions with the str_detect() function from stringr. These two packages have been loaded for you, and the rstudioconf dataset is still available in your workspace.

แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร

Intermediate Functional Programming with purrr

ดูคอร์ส

แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ

ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์

# Complete the function definition
str_prop_detected <- function(string, pattern) {
  string %>%
    # Detect the pattern
    ___(___) %>%
    # Calculate the mean
    ___()
} 

# Create flatten_and_compact()
flatten_and_compact <- ___(___, ___)
แก้ไขและรันโค้ด