-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcleaningScript.R
More file actions
52 lines (39 loc) · 1.86 KB
/
cleaningScript.R
File metadata and controls
52 lines (39 loc) · 1.86 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
49
50
51
52
library(sf)
library(collapse)
library(stringr)
library(ggplot2)
#join XY data to data frame that is used for mapping
agri_data <- data.table::fread("new_agri_data.csv")
world_shape <- read_sf("world_shapefile") |> st_drop_geometry()
economy_sf <- rnaturalearthdata::countries50 |> st_as_sf() |> st_drop_geometry()
#grab the economy data from economy_sf
world_shape <- left_join(world_shape, economy_sf[c("iso_a3", "economy")], by = c( "ISO3" = "iso_a3"))
#convert to integer for easy binding of the similarities of the data with world_shape
agri_data$`Area Code (M49)` <- as.integer(sub("'", "", agri_data$`Area Code (M49)`))
#get rid of area's that don't have lat or are not actually countries
agri_data <- agri_data |>
left_join(world_shape[, c("UN", "LON", "LAT", "economy")], by = c("Area Code (M49)" = "UN")) |>
sbt(!is.na(LAT) & Element %in% c("Area harvested","Yield","Production"))
# #take geometry from world sf object, get the X and Y Coordinates and append the columns
# worldXY <- countriesData |>
# st_as_sf() |>
# st_geometry() |>
# st_make_valid() |>
# st_centroid() |>
# unlist() |>
# matrix(ncol = 2, byrow = T) |>
# as.data.frame() |>
# setNames(c("X", "Y")) |>
# cbind(name = countriesData$name, economy = countriesData$economy)
#
#
# newJoined <- joined|>
# fuzzyjoin::fuzzy_left_join(worldXY , match_fun = list(stringr::str_detect), by = c("Area" = "name")) |>
# (\(x){
# x |>
# ss(!x$Area %in% c("Africa", "Asia", "Europe", "Americas", "Oceania", "World", "South-eastern Asia") &
# x$Element %in% c("Area harvested","Yield","Production") & !is.na(x$Area),
# !str_detect(names(x), "F$|\\sCode|Unit|name"))
# })()
agri_data[, c("Item", "Area")] <- Map(\(x,y,z) str_replace_all(x, y, z),list(agri_data$Item, agri_data$Area),c("\xe9","C\xf4"),c("e", "Co"))
fwrite(agri_data,"cleanedFrame2.csv")