Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Trying to run R Tutorial script and getting an Error in 'left_join():

karlioso

New Coder
I a trying to run this and get an error:

Code:
# Nonprofit Data Analysis in R----
# Section 2: Loading, Joining, and Exploring Data----

#1.0 Load packages----

library(janitor)
library(lubridate)
library(tidyverse)
library(readxl)
library(tidyquant)

#2.0 Load data----

reports <- reports_db %>%
clean_names()

clients <- clients_db %>%
clean_names()

employees <- employees_db %>%
clean_names()

locations <- locations_db %>%
clean_names()

cw_df <- reports %>%
left_join(clients, by= "client_id") %>%
left_join(locations, by= c("location" = "location_id")) %>%
left_join(employees, by= c("employee_reporting" = "employee_id"))

cw_df %>%
select(report_date, dob, admission_date, client_id) %>%
glimpse()

cw_df %>%
select(contains("date"), dob) %>%
glimpse()

cw_df %>%
select(report_date:report_level, location_type:employee_title) %>%
glimpse()

cw_df %>%
select(-report_date, -(starts_with("locations")))

cw_df %>%
select_if(is.numeric)

cw_df %>%
select(starts_with("employee"), everything()) %>%
glimpse()

# Mutate----
cw_df %>%
group_by(client_id) %>%
mutate(report_count = n()) %>%
ungroup() %>%
mutate(report_count_binned = ntile(report_count, 3)) %>%
mutate(report_count_cat = case_when(
report_count > quantile(report_count, .66) ~ "High",
report_count > quantile(report_count, .33) ~ "Medium",
report_count <= quantile(report_count, .33) ~ "Low",
TRUE ~ "NA") %>% as.factor()) %>%
mutate(foster_care = if_else(program_admitted %in% c("foster", "ther. foster"), "Yes",
"No")) %>%
mutate(foster_care = case_when(
program_admitted %in% c("ther. foster", "foster") ~ "Yes",
program_admitted == "residential" ~ "No",
TRUE ~ "Other") %>% as.factor()) %>%
mutate(incident_cat = case_when(
incident_type %>% str_to_lower() %>% str_detect("behavior") ~ "Behavior",
incident_type %>% str_to_lower() %>% str_detect("abuse") ~ "Abuse",
TRUE ~ "Other") %>% as.factor()) %>%
View()


table(cw_df$incident_type)
--------------------------------------

HERE is the ERROR:
Error in left_join():! Join columns in y must be present in the data.✖ Problem with client_id.
 
Last edited by a moderator:

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom