Project 1 Assignment

To generate the desired CSV file from the provided text file with chess tournament results, you can use R Markdown and the `readLines()` function in R to read the text file. Then, manipulate and extract the required information using string operations and regular expressions. Finally, save the resulting data as a CSV file.

First, I wrote the component that was able to parse the data from the text file into a CSV. Afterwards I replaced the text input with the url of the textfile ‘tournamentinfo.txt’. The original url was long, so I used tinyurl, which is a url shortening service.

# Read the text file
text <- readLines("https://tinyurl.com/4rm79cav")
## Warning in readLines("https://tinyurl.com/4rm79cav"): incomplete final line
## found on 'https://tinyurl.com/4rm79cav'
# Create an empty matrix to store the extracted information
player_info <- matrix(ncol = 5)
colnames(player_info) <- c("Player's Name", "Player's State", "Total Number of Points", "Player's Pre-Rating", "Average Pre Chess Rating of Opponents")

# Loop through each line in the text
for (i in 1:length(text)) {
  line <- text[i]
  
  # Extract the required information using string operations and regular expressions
  if (grepl("^\\s*\\d+ \\| ", line)) {
    player_name <- trimws(substring(line, 12, 32))
    player_state <- trimws(substring(line, 35, 36))
    total_points <- trimws(substring(line, 39, 42))
    player_pre_rating <- trimws(substring(line, 45, 49))
    average_rating <- trimws(substring(line, 54))
    
    # Add the extracted information to the player_info matrix
    player_info <- rbind(player_info, c(player_name, player_state, total_points, player_pre_rating, average_rating))
  }
}

# Convert the matrix to a data frame
player_df <- as.data.frame(player_info[-1, ], stringsAsFactors = FALSE)

# Save the data frame as a CSV file
write.csv(player_df, file = "chess_results.csv", row.names = FALSE)