library(readxl)
# Import Excel file from the data folder
Roundabouts <- read_excel("Raw data Roundabout copy.xlsx")
Roundabouts
## # A tibble: 27,887 × 18
## name address town_city county_area state_region country lat long type
## <chr> <chr> <chr> <chr> <chr> <chr> <dbl> <dbl> <chr>
## 1 Arapah… Boulde… Boulder Boulder Co. CO United… -105. 40.0 Traf…
## 2 Arapah… Boulde… Boulder Boulder Co. CO United… -105. 40.0 Traf…
## 3 10th A… Belmar… Belmar Monmouth C… NJ United… -74.0 40.2 Traf…
## 4 10th A… Belmar… Belmar Monmouth C… NJ United… -74.0 40.2 Traf…
## 5 Bagley… Seattl… Seattle King Co. WA United… -122. 47.7 Traf…
## 6 8th Av… Seattl… Seattle King Co. WA United… -122. 47.7 Traf…
## 7 8th Av… Seattl… Seattle King Co. WA United… -122. 47.7 Traf…
## 8 8th Av… Seattl… Seattle King Co. WA United… -122. 47.7 Traf…
## 9 Kendal… Madiso… Madison Dane Co. WI United… -89.4 43.1 Traf…
## 10 Bagley… Seattl… Seattle King Co. WA United… -122. 47.7 Traf…
## # ℹ 27,877 more rows
## # ℹ 9 more variables: status <chr>, year_completed <dbl>, approaches <dbl>,
## # driveways <dbl>, lane_type <chr>, functional_class <chr>,
## # control_type <chr>, other_control_type <chr>, previous_control_type <chr>
# Display dataset
Roundabouts
## # A tibble: 27,887 × 18
## name address town_city county_area state_region country lat long type
## <chr> <chr> <chr> <chr> <chr> <chr> <dbl> <dbl> <chr>
## 1 Arapah… Boulde… Boulder Boulder Co. CO United… -105. 40.0 Traf…
## 2 Arapah… Boulde… Boulder Boulder Co. CO United… -105. 40.0 Traf…
## 3 10th A… Belmar… Belmar Monmouth C… NJ United… -74.0 40.2 Traf…
## 4 10th A… Belmar… Belmar Monmouth C… NJ United… -74.0 40.2 Traf…
## 5 Bagley… Seattl… Seattle King Co. WA United… -122. 47.7 Traf…
## 6 8th Av… Seattl… Seattle King Co. WA United… -122. 47.7 Traf…
## 7 8th Av… Seattl… Seattle King Co. WA United… -122. 47.7 Traf…
## 8 8th Av… Seattl… Seattle King Co. WA United… -122. 47.7 Traf…
## 9 Kendal… Madiso… Madison Dane Co. WI United… -89.4 43.1 Traf…
## 10 Bagley… Seattl… Seattle King Co. WA United… -122. 47.7 Traf…
## # ℹ 27,877 more rows
## # ℹ 9 more variables: status <chr>, year_completed <dbl>, approaches <dbl>,
## # driveways <dbl>, lane_type <chr>, functional_class <chr>,
## # control_type <chr>, other_control_type <chr>, previous_control_type <chr>
# Check dataset dimensions
dim(Roundabouts)
## [1] 27887 18
ncol(Roundabouts)
## [1] 18
colnames(Roundabouts)
## [1] "name" "address" "town_city"
## [4] "county_area" "state_region" "country"
## [7] "lat" "long" "type"
## [10] "status" "year_completed" "approaches"
## [13] "driveways" "lane_type" "functional_class"
## [16] "control_type" "other_control_type" "previous_control_type"
count_cols <- function(df) {
num_cols <- ncol(df)
return(num_cols)
}
Roundabouts %>% count_cols()
## [1] 18
# Adding arguments for details of operation
count_cols_checked <- function(df, expected_cols = 18) {
actual_cols <- ncol(df)
if (actual_cols == expected_cols) {
message("Column count matches expectations")
print(actual_cols)
} else {
stop("Dataset does not have the expected number of columns. Function stopped")
}
}
Roundabouts %>% count_cols_checked(expected_cols = 18)
## Column count matches expectations
## [1] 18
nrow(Roundabouts)
## [1] 27887
nrow(Roundabouts %>% filter(state_region == "CO"))
## [1] 624
count_state_region_rows <- function(df, target_state_region = "CO") {
subset_df <- df %>% filter(state_region == target_state_region)
total_rows <- nrow(subset_df)
return(total_rows)
}
Roundabouts %>% count_state_region_rows(target_state_region = "CO")
## [1] 624
Create your own.
nrow(Roundabouts %>% filter(country == "United States"))
## [1] 12952
nrow(Roundabouts %>% filter(approaches > 3))
## [1] 18384
count_high_approach_rows <- function(df, threshold = 3) {
high_approach_df <- df %>%
filter(approaches > threshold)
row_count <- nrow(high_approach_df)
if (row_count > 500) {
message("High count of intersections above the threshold")
print(row_count)
} else if (row_count > 0) {
warning("Low count of intersections above the threshold")
print(row_count)
} else {
stop("No rows found above the threshold")
}
return(high_approach_df)
}
high_approach_circles <- Roundabouts %>%
count_high_approach_rows(threshold = 3)
## High count of intersections above the threshold
## [1] 18384