-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract_fude.R
65 lines (55 loc) · 2.03 KB
/
extract_fude.R
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
53
54
55
56
57
58
59
60
61
62
63
64
65
#' Extract specified Fude Polygon data
#'
#' @description
#' `extract_fude()` extracts the specified data from the list returned by
#' [read_fude()].
#' @param data
#' List of [sf::sf()] objects.
#' @param year
#' Years to be extracted.
#' @param city
#' Local government names or codes to be extracted.
#' @param list
#' logical. If `FALSE`, the object to be extracted is no longer a list.
#' @returns A list of [sf::sf()] object(s).
#' @seealso [read_fude()].
#'
#' @examples
#' path <- system.file("extdata", "castle.zip", package = "fude")
#' d <- read_fude(path, stringsAsFactors = FALSE, quiet = TRUE)
#' d2 <- extract_fude(d, year = 2022)
#'
#' @export
extract_fude <- function(data, year = NULL, city = NULL, list = TRUE) {
if (is.null(year) & is.null(city)) {
stop("Specify either `year` or `city`.")
}
if (!is.null(city)) {
if (is.null(year)) {
year <- unique(ls_fude(data)$year)
}
selected_names <- NULL
for (i in year) {
data_i <- ls_fude(data)[ls_fude(data)$year == i, ]
matching_idx1 <- match(city, data_i$local_government_cd)
matching_idx2 <- match(sub("(\u5e02|\u533a|\u753a|\u6751)$", "", city),
sub("(\u5e02|\u533a|\u753a|\u6751)$", "", data_i$city_kanji))
matching_idx3 <- match(tolower(gsub("-SHI|-KU|-CHO|-MACHI|-SON|-MURA", "", city, ignore.case = TRUE)),
tolower(gsub("-SHI|-KU|-CHO|-MACHI|-SON|-MURA", "", data_i$romaji, ignore.case = TRUE)))
matching_idx4 <- match(city, data_i$names)
matching_idx <- unique(c(matching_idx1, matching_idx2, matching_idx3, matching_idx4))
selected_names <- c(selected_names, data_i$full_names[stats::na.omit(matching_idx)])
}
} else {
selected_names <- grep(paste0(year, collapse = "|"), names(data), value = TRUE)
}
if (list == TRUE) {
x <- data[selected_names]
} else {
if (length(selected_names) > 1) {
stop("`list` must be TRUE if there are multiple objects to be extracted.")
}
x <- data[[selected_names]]
}
return(x)
}