my apologies I am very new to coding.
I have two dataframes (unique*_*pt_all, denom_2011) with the same columns but different number of rows of patients:
> colnames(unique_pt_all)
> [1] "DESY_SORT_KEY" "STATE_CODE" "COUNTY_CODE"
> [4] "SEX_CODE" "RACE_CODE" "AGE"
> [7] "REFERENCE_YEAR" "C_DRY_EYE_COUNT" "C_SJOGRENS_COUNT"
> [10] "C_AA_KERATACONJUNCTIVITIS_COUNT" "C_DIABETES_MELLITUS_COUNT" "C_POA_GLAUCOMA_COUNT"
> [13] "C_CICATRIZING_COUNT" "C_CORNEAL_DISORDER_LENS_COUNT" "C_CORNEAL_ULCER_COUNT"
> [16] "C_CATARACT_COUNT" "C_KERATOPLASTY_COUNT" "C_PTERYGIUM_COUNT"
> [19] "C_CORNEAL_ULCER_MIN_DT" "C_CORNEAL_ULCER_MAX_DT" "C_CATARACT_MIN_DT"
> [22] "C_KERATOPLASTY_MIN_DT" "C_PTERYGIUM_MIN_DT"
I want to add all of the values in columns 8-18 from dataframe "denom_2011" to the existing values in the same columns in dataframe "unique_pt_all" if the value in column "DESY_SORT_KEY" in "denom_2011" match AND the value in column "C_CORNEAL_ULCER_COUNT" is <= 2 in dataframe "unique_pt_all".
I've tried:
columns_to_add <- c(
"C_DRY_EYE_COUNT",
"C_SJOGRENS_COUNT",
"C_AA_KERATACONJUNCTIVITIS_COUNT",
"C_DIABETES_MELLITUS_COUNT",
"C_POA_GLAUCOMA_COUNT",
"C_CICATRIZING_COUNT",
"C_CORNEAL_DISORDER_LENS_COUNT",
"C_CORNEAL_ULCER_COUNT",
"C_CATARACT_COUNT",
"C_KERATOPLASTY_COUNT",
"C_PTERYGIUM_COUNT"
)
# Update values based on conditions
unique_pt_all <- unique_pt_all %>%
left_join(denom_2011 %>% filter(C_CORNEAL_ULCER_COUNT <= 2),
by = "DESY_SORT_KEY",
suffix = c("_unique", "_denom")) %>%
mutate(across(all_of(columns_to_add),
~ifelse(is.na(.x_unique), .x, .x_unique + .x_denom))) %>%
select(-ends_with("_denom"))
However I'm getting the error:
Error in `mutate()`:
ℹ In argument: `across(...)`.
Caused by error in `all_of()`:
! Can't subset columns that don't exist.
✖ Columns `C_DRY_EYE_COUNT`, `C_SJOGRENS_COUNT`, `C_AA_KERATACONJUNCTIVITIS_COUNT`, `C_DIABETES_MELLITUS_COUNT`, `C_POA_GLAUCOMA_COUNT`, etc. don't exist.
Edit: Including the column names after left join:
colnames(unique_pt_all) [1] "DESY_SORT_KEY" "STATE_CODE_unique"
[3] "COUNTY_CODE_unique" "SEX_CODE_unique"
[5] "RACE_CODE_unique" "AGE_unique"
[7] "REFERENCE_YEAR_unique" "C_DRY_EYE_COUNT_unique"
[9] "C_SJOGRENS_COUNT_unique" "C_AA_KERATACONJUNCTIVITIS_COUNT_unique" [11] "C_DIABETES_MELLITUS_COUNT_unique" "C_POA_GLAUCOMA_COUNT_unique"
[13] "C_CICATRIZING_COUNT_unique" "C_CORNEAL_DISORDER_LENS_COUNT_unique"
[15] "C_CORNEAL_ULCER_COUNT_unique" "C_CATARACT_COUNT_unique"
[17] "C_KERATOPLASTY_COUNT_unique" "C_PTERYGIUM_COUNT_unique"
[19] "C_CORNEAL_ULCER_MIN_DT_unique" "C_CORNEAL_ULCER_MAX_DT_unique"
[21] "C_CATARACT_MIN_DT_unique" "C_KERATOPLASTY_MIN_DT_unique"
[23] "C_PTERYGIUM_MIN_DT_unique" "STATE_CODE_denom"
[25] "COUNTY_CODE_denom" "SEX_CODE_denom"
[27] "RACE_CODE_denom" "AGE_denom"
[29] "REFERENCE_YEAR_denom" "C_DRY_EYE_COUNT_denom"
[31] "C_SJOGRENS_COUNT_denom" "C_AA_KERATACONJUNCTIVITIS_COUNT_denom" [33] "C_DIABETES_MELLITUS_COUNT_denom" "C_POA_GLAUCOMA_COUNT_denom"
[35] "C_CICATRIZING_COUNT_denom" "C_CORNEAL_DISORDER_LENS_COUNT_denom"
[37] "C_CORNEAL_ULCER_COUNT_denom" "C_CATARACT_COUNT_denom"
[39] "C_KERATOPLASTY_COUNT_denom" "C_PTERYGIUM_COUNT_denom"
[41] "C_CORNEAL_ULCER_MIN_DT_denom" "C_CORNEAL_ULCER_MAX_DT_denom"
[43] "C_CATARACT_MIN_DT_denom" "C_KERATOPLASTY_MIN_DT_denom"
[45] "C_PTERYGIUM_MIN_DT_denom"