I’m currently working on converting some postal codes into latitude and longitude coordinates. I’ve successfully imported the PCCF, but I’ve run into an issue. The smallest geography unit in the PCCF is the community name, which is more granular than my data requires. This means that a single postal code can correspond to multiple community names, each with its own latitude and longitude, as shown in the attached image.
enter image description here
My question is: Is there an alternative method to calculate the latitude and longitude for each postal code without relying on the community name? For example, can I use a central or average value of the latitudes and longitudes corresponding to the same postal code?
Currently, I’ve tried the following code without success:
##PCCF
library(data.table)
library(readr)
column_widths <- c(6,3,2,4,7,70,3,3,3,1,7,2,4,5,4,1,8,3,1,11,13,1,1,30,1,1,8,8,1,3,1,1)
pccf_fwf <- read_fwf("/Users/langbai/Desktop/PCCF_FCCP_V2403_2021.txt", col_positions = fwf_widths(column_widths))
column_names <- c("Postal_code", "FSA", "PR", "CDuid", "CSDuid", "CSDname", "CSDtype", "CCScode", "SAC", "SACtype", "Ctname","ER", "DPL", "FED13iud", "POP_CNTR_RA", "POP_CNTR_RA_type", "PRCDDA", "Dissemination_block", "Rep_Pt_Type", "LAT", "LONG", "SLI","PCtype", "Comm_Name", "DMT", "H_DMT", "Birth_Date", "Ret_Date", "PO", "QI", "Source", "POP_CENTR_RA_SIZE_CLASS")
names(pccf_fwf) <- column_names
Merged Data
pccf_fwf <- pccf_fwf %>%
mutate(CSDuid = as.character(CSDuid))
matched_data <- shapefile_data %>%
left_join(pccf_fwf, by = c("CSDUID" = "CSDuid"))
final_merged_data <- matched_data %>%
left_join(df, by = "Postal_code")
final_data_filtered <- final_merged_data %>%
filter(!is.na(number_of_farms))%>%
arrange(desc(number_of_farms))
final_data_frame <- final_data_filtered %>%
st_set_geometry(NULL) %>%
as.data.frame()
final_data_filtered_selected <- final_data_frame %>%
select(CSDUID, CSDNAME, LAT, LONG, number_of_farms, Postal_code) %>%
distinct()
final_grouped_data <- final_data_filtered_selected %>%
group_by(Postal_code)
Any advice or suggestions would be greatly appreciated!
Emma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.