-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_cleaner.R
More file actions
32 lines (27 loc) · 1.11 KB
/
data_cleaner.R
File metadata and controls
32 lines (27 loc) · 1.11 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
library(tidyverse)
# Path to the file
file_path <- "data/cleaned_CO2_emissions,_total_(KtCO2).csv"
# Step 1: Load the data and inspect structure
raw_data <- read_csv(file_path)
print("Raw Data:")
print(head(raw_data))
print("Column Names in Raw Data:")
print(colnames(raw_data))
# Step 2: Reshape the data
reshaped_data <- raw_data %>%
rename_with(~ gsub("\\s+", "_", .x)) %>% # Replace spaces with underscores
rename_with(~ gsub("[()%,]", "", .x)) %>% # Remove special characters
pivot_longer(
cols = starts_with("19") | starts_with("20"), # Select year columns dynamically
names_to = "Year", # New column for year
values_to = "Value" # New column for the data
) %>%
mutate(Year = as.integer(Year)) # Ensure Year is numeric
# Step 3: Rename the Value column to Total_CO2
reshaped_data <- reshaped_data %>%
rename(Total_CO2 = Value)
# Step 4: Inspect reshaped data
print("Reshaped Data:")
print(head(reshaped_data))
print("Column Names in Reshaped Data:")
print(colnames(reshaped_data))