Import your data
data <- read_excel("CliffordSalmonidDataApply11.xlsx")
data
## # A tibble: 2,808 × 9
## species date geo_group region losses dead discarded escaped
## <chr> <dttm> <chr> <chr> <dbl> <dbl> <dbl> <dbl>
## 1 salmon 2020-01-01 00:00:00 area 1 31425 28126 3299 0
## 2 salmon 2020-01-01 00:00:00 area 2 324116 277888 46113 0
## 3 salmon 2020-01-01 00:00:00 area 3 844829 776983 63770 0
## 4 salmon 2020-01-01 00:00:00 area 4 676852 623159 51823 0
## 5 salmon 2020-01-01 00:00:00 area 5 109269 97627 11424 0
## 6 salmon 2020-01-01 00:00:00 area 6 548921 531193 15710 0
## 7 salmon 2020-01-01 00:00:00 area 7 231487 228847 2640 0
## 8 salmon 2020-01-01 00:00:00 area 8 442659 431218 11228 0
## 9 salmon 2020-01-01 00:00:00 area 9 311127 308298 2212 0
## 10 salmon 2020-01-01 00:00:00 area 10 288849 281808 3120 0
## # ℹ 2,798 more rows
## # ℹ 1 more variable: other <dbl>
Create Data frame functions
Example 1: count columns
code snippets
ncol(data)
## [1] 9
colnames(data)
## [1] "species" "date" "geo_group" "region" "losses" "dead"
## [7] "discarded" "escaped" "other"
Turn them into a function
count_cols <- function(df) {
num_cols <- ncol(df)
return(num_cols)
}
data %>% count_cols()
## [1] 9
Adding arguments for details of operation
count_cols_checked <- function(df, expected_cols = 9, ...) {
actual_cols <- ncol(df, ...)
if (actual_cols == expected_cols) {
message("Column count matches expectations")
print(actual_cols)
} else if (actual_cols > expected_cols) {
warning("Dataset has more columns than expected")
print(actual_cols)
} else {
stop("Dataset has fewer columns than required. Function stopped")
}
}
data %>% count_cols_checked(expected_cols = 9)
## Column count matches expectations
## [1] 9
Example 2: count rows
code snippets
nrow(data)
## [1] 2808
nrow(data %>% filter(species == "salmon"))
## [1] 1656
Turn them into a function
count_species_rows <- function(df, target_species = "salmon") {
subset_df <- df %>% filter(species == target_species)
total_rows <- nrow(subset_df)
return(total_rows)
}
data %>% count_species_rows(target_species = "salmon")
## [1] 1656
Example 3: count rows
code snippets
nrow(data %>% filter(losses > 100000))
## [1] 1620
Turn them into a function
count_high_loss_rows <- function(df, threshold = 100000) {
high_loss_df <- df %>% filter(losses > threshold)
row_count <- nrow(high_loss_df)
if (row_count > 500) {
message("High volume of high losses")
print(row_count)
} else if (row_count > 0) {
warning("High losses but not detrimental")
print(row_count)
} else {
stop("Error in high losses value")
}
return(high_loss_df)
}
data %>% count_high_loss_rows(threshold = 100000)
## High volume of high losses
## [1] 1620
## # A tibble: 1,620 × 9
## species date geo_group region losses dead discarded escaped
## <chr> <dttm> <chr> <chr> <dbl> <dbl> <dbl> <dbl>
## 1 salmon 2020-01-01 00:00:00 area 2 324116 277888 46113 0
## 2 salmon 2020-01-01 00:00:00 area 3 844829 776983 63770 0
## 3 salmon 2020-01-01 00:00:00 area 4 676852 623159 51823 0
## 4 salmon 2020-01-01 00:00:00 area 5 109269 97627 11424 0
## 5 salmon 2020-01-01 00:00:00 area 6 548921 531193 15710 0
## 6 salmon 2020-01-01 00:00:00 area 7 231487 228847 2640 0
## 7 salmon 2020-01-01 00:00:00 area 8 442659 431218 11228 0
## 8 salmon 2020-01-01 00:00:00 area 9 311127 308298 2212 0
## 9 salmon 2020-01-01 00:00:00 area 10 288849 281808 3120 0
## 10 salmon 2020-01-01 00:00:00 area 11 565676 516146 4761 0
## # ℹ 1,610 more rows
## # ℹ 1 more variable: other <dbl>