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

Extracting the first digit I

To address the question of voter fraud, begin by creating a new column of data containing the first digit of the total number of votes cast. For this, you'll need a custom function which we've created for you called get_first(). The core of this function is substr(), which will take a string and extract a section of it called a substring.

Once you create a new variable containing only the first digit, you can get a sense of how close it follows Benford's Law by constructing a bar plot.

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

Inference for Categorical Data in R

ดูคอร์ส

คำแนะนำการฝึกหัด

  • Take a look at how get_first() works by simply typing the name of the function (with no parentheses). All it does is fiddle with the output from substr() so that it's a factor.
  • Mutate a new column in the iran data frame called first_digit that contains the first digit of city by city total votes cast.
  • Check to see that get_first() worked. From the iran data, select off the columns total_votes_cast and first_digit and print them to the screen.
  • Construct a bar plot to visualize the distribution of the first digit.

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

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

# Print get_first
get_first

# Create first_digit variable
iran <- iran %>%
  ___
  
# Check if get_first worked
___ %>%
  ___

# Construct bar plot
___ +
  # Add bar layer
  ___
แก้ไขและรันโค้ด