Module # 3 Data.frame

 

Data Frame with 2016 Election Results

1) This week I read chapters three through five of "The art of R programming" covering matrices, arrays, lists and of course data frames. This is not the first time I have used data frames in RStuido and I get the feeling it won't be the last. 

2) I was given the following piece of code and I am supposed to discuss my findings about it. The following code is completely made up and does not show the real election results.

> Name <- c("Jeb", “Donald”, "Ted”, “Marco” “Carly”, “Hillary”, “Berine”)

> ABC political poll results <- c(4, 62 51, 21, 2, 14, 15)

> CBS political poll results <- c(12, 75, 43, 19, 1, 21, 19) 


3) With this code I made a data frame, created a totals column for that data frame where which I could then use to sort the candidates by votes so that the candidates with the most votes would appear in descending order on a list. I then thought I could take it further by putting it on a pie chart. I remember putting data on line charts before and figured it should not be that different. So I put the election results on a pie chart that is easily readable. 

4) The R code I used:
# Module 3 data.frame

Name <- c("Jeb", "Donald", "Ted", "Marco", "Carly", "Hillary", "Bernie")
ABC_poll_results <- c(4, 62, 51, 21, 2, 14, 15)
CBS_poll_results <- c(12, 75, 43, 19, 1, 21, 19)

# Create the data frame
poll_results <- data.frame(Name, CBS_poll_results, ABC_poll_results)

# Add the 'Total' column (sum of CBS and ABC poll results)
poll_results$Total <- CBS_poll_results + ABC_poll_results

# Print the data frame with the new column
print(poll_results)


# Sort by 'Total' in descending order
poll_results_sorted <- poll_results[order(-poll_results$Total), ]

# Print the sorted data frame
print(poll_results_sorted)

#Creating a pie chart
# Create the labels to include candidate name and total votes
labels <- paste(poll_results$Name, poll_results$Total, sep = ": ")

# Create a pie chart using the 'Total' column, with names and vote counts as labels
pie(poll_results$Total, labels = labels, main = "Total Votes by Candidate", col = rainbow(length(poll_results$Name)))

4) The poll results in the console: 


> print(poll_results)
     Name CBS_poll_results ABC_poll_results Total
1     Jeb               12                4    16
2  Donald               75               62   137
3     Ted               43               51    94
4   Marco               19               21    40
5   Carly                1                2     3
6 Hillary               21               14    35
7  Bernie               19               15    34


> print(poll_results_sorted)
     Name CBS_poll_results ABC_poll_results Total
2  Donald               75               62   137
3     Ted               43               51    94
4   Marco               19               21    40
6 Hillary               21               14    35
7  Bernie               19               15    34
1     Jeb               12                4    16
5   Carly                1                2     3


5) The pie chart I created:




Comments

Popular posts from this blog

Module # 10 Building my own R package

Module # 13 Shiny Web App

Module # 4 Programming Structure