-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.r
More file actions
48 lines (36 loc) · 1.39 KB
/
main.r
File metadata and controls
48 lines (36 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# Load necessary packages (if not already installed)
if (!require("jsonlite")) {
install.packages("jsonlite")
}
library(jsonlite)
# Read Data from JSON File
articles_data <- fromJSON("data/articles.json")
# Access the total number of results
total_results <- articles_data$totalResults
# Access the articles as a data frame
articles_df <- as.data.frame(articles_data$articles)
# View the structure of the data frame
str(articles_df)
# View the first few rows of the data frame
head(articles_df)
# Check for missing values
colSums(is.na(articles_df))
# Count of articles from each source
source_counts <- table(articles_df$source)
source_counts_df <- as.data.frame(source_counts)
colnames(source_counts_df) <- c("Source", "Count") # Rename columns for clarity
# Write the data frame to a CSV file
write.csv(source_counts_df, file = "data/source_counts.csv", row.names = FALSE)
# Print out the data frame to confirm
print(source_counts_df)
# Adjust bottom margin to accommodate long labels
par(mar = c(7, 2, 3, 3) + 0.9)
# Create a bar plot for source counts (sorted in descending order)
barplot(sort(source_counts, decreasing = TRUE),
main = "Number of Articles by Source",
ylab = "Count",
las = 2,
cex.names = 0.6)
mtext("Source", side = 1, line = 5, cex = 0.8)
# Move the Rplots.pdf file to the data folder
file.rename("Rplots.pdf", "data/Rplots.pdf")