generated from bluegreen-labs/R_project_template
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
# Generated by roxygen2: do not edit by hand | ||
|
||
export(align_events) | ||
export(merge_normalized_differences) | ||
export(normalized_difference) | ||
export(read_appeears) | ||
export(read_ml_data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
|
||
#' Merge / calculate difference ratios | ||
#' | ||
#' Calculate normalized difference ratios | ||
#' for all possible "sur_refl" band combinations | ||
#' | ||
#' @param ml_df a machine learning data frame with "sur_refl" prefix column | ||
#' names apply the band ratios to | ||
#' | ||
#' @return A machine learning data frame with normalized band ratios appended | ||
#' @export | ||
|
||
merge_normalized_differences <- function(ml_df){ | ||
|
||
# split out surface reflectance values | ||
sur_refl <- ml_df |> | ||
dplyr::select( | ||
starts_with("sur_refl") | ||
) | ||
|
||
# calculate all normalized difference ratios | ||
ndr <- lapply(names(sur_refl), function(band){ | ||
|
||
# split out reference band | ||
band_1 <- sur_refl |> | ||
dplyr::select( | ||
all_of(band) | ||
) | ||
|
||
# mutate over all other collumns | ||
df <- sur_refl |> | ||
dplyr::select( | ||
!all_of(band) | ||
) | ||
|
||
# wrangle all column names | ||
cols <- gsub("sur_refl_", "", names(df)) | ||
ref_col <- gsub("sur_refl_", "", names(band_1)) | ||
|
||
df <- df |> | ||
dplyr::transmute_all( | ||
function(x){as.vector(normalized_difference(band_1, x))} | ||
) | ||
|
||
# add clean column names | ||
colnames(df) <- paste(ref_col, cols, sep = "_") | ||
return(df) | ||
}) | ||
|
||
# bind columns in one big tibble | ||
ndr <- dplyr::bind_cols(ndr) | ||
|
||
# merge with original data | ||
ml_df <- bind_cols(ml_df, ndr) | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
|
||
#' Normalized difference | ||
#' | ||
#' Calculates normalized difference for two (arbitrary) bands. The difference | ||
#' is taken as band 1 - band 2 | ||
#' | ||
#' @param band_1 first band | ||
#' @param band_2 second band | ||
#' | ||
#' @return normalized difference index for two bands | ||
#' @export | ||
|
||
normalized_difference <- function(band_1, band_2){ | ||
ndr <- (band_1 - band_2)/(band_1 + band_2) | ||
as.matrix(ndr) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.