Error in Code for select()

Not sure how to fix this error in my code.

limit data to 2015 basic and safe water

library(package = "tidyverse")
water.cleaned <- water %>%
select(Country, Total, Total_1) %>%
rename(Country = 'Country', perc.basic2015water = 'Total', perc.safe2015water = 'Total_1')

Error in select():
! Can't subset columns that don't exist.
:heavy_multiplication_x: Column Total doesn't exist.
Run rlang::last_error() to see where the error occurred.

rlang::last_error()
<error/vctrs_error_subscript_oob>
Error in select():
! Can't subset columns that don't exist.
:heavy_multiplication_x: Column Total doesn't exist.


Backtrace:

  1. water %>% select(Country, Total, Total_1) %>% ...
  2. dplyr:::select.data.frame(., Country, Total, Total_1)
    Run rlang::last_trace() to see the full context.

rlang::last_trace()
<error/vctrs_error_subscript_oob>
Error in select():
! Can't subset columns that don't exist.
:heavy_multiplication_x: Column Total doesn't exist.


Backtrace:

  1. ├─water %>% select(Country, Total, Total_1) %>% ...
  2. ├─dplyr::rename(...)
  3. ├─dplyr::select(., Country, Total, Total_1)
  4. ├─dplyr:::select.data.frame(., Country, Total, Total_1)
  5. │ └─tidyselect::eval_select(expr(c(...)), data = .data, error_call = error_call)
  6. │ └─tidyselect:::eval_select_impl(...)
  7. │ ├─tidyselect:::with_subscript_errors(...)
  8. │ │ └─rlang::try_fetch(...)
  9. │ │ └─base::withCallingHandlers(...)
  10. │ └─tidyselect:::vars_select_eval(...)
  11. │ └─tidyselect:::walk_data_tree(expr, data_mask, context_mask)
  12. │ └─tidyselect:::eval_c(expr, data_mask, context_mask)
  13. │ └─tidyselect:::reduce_sels(node, data_mask, context_mask, init = init)
  14. │ └─tidyselect:::walk_data_tree(new, data_mask, context_mask)
  15. │ └─tidyselect:::as_indices_sel_impl(...)
  16. │ └─tidyselect:::as_indices_impl(...)
  17. │ └─tidyselect:::chr_as_locations(x, vars, call = call, arg = arg)
  18. │ └─vctrs::vec_as_location(...)
  19. └─vctrs (local) <fn>()
  20. └─vctrs:::stop_subscript_oob(...)
  21. └─vctrs:::stop_subscript(...)
    
  22.   └─rlang::abort(...)
    

Please show the output of

colnames(water)

colnames(water)
[1] "Country" "Total...2" "Urban...3" "Rural...4" "Total...5" "Urban...6" "Rural...7"
[8] "Total...8" "Urban...9" "Rural...10" "Total...11" "Urban...12" "Rural...13" "Total...14"
[15] "Urban...15" "Rural...16" "Total...17" "Urban...18" "Rural...19" "Total...20" "Urban...21"
[22] "Rural...22" "Total...23" "Urban...24" "Rural...25" "Total...26" "Urban...27" "Rural...28"
[29] "Total...29" "Urban...30" "Rural...31" "Total...32" "Urban...33" "Rural...34" "Total...35"
[36] "Urban...36" "Rural...37" "Total...38" "Urban...39" "Rural...40" "Total...41" "Urban...42"
[43] "Rural...43" "Total...44" "Urban...45" "Rural...46" "Total...47" "Urban...48" "Rural...49"
[50] "Total...50" "Urban...51" "Rural...52" "Total...53" "Urban...54" "Rural...55" "Total...56"
[57] "Urban...57" "Rural...58" "Total...59" "Urban...60" "Rural...61" "Total...62" "Urban...63"
[64] "Rural...64" "Total...65" "Urban...66" "Rural...67" "Total...68" "Urban...69" "Rural...70"
[71] "Total...71" "Urban...72" "Rural...73" "Total...74" "Urban...75" "Rural...76" "Total...77"
[78] "Urban...78" "Rural...79" "Total...80" "Urban...81" "Rural...82" "Total...83" "Urban...84"
[85] "Rural...85" "Total...86" "Urban...87" "Rural...88" "Total...89" "Urban...90" "Rural...91"
[92] "Total...92" "Urban...93" "Rural...94" "Total...95" "Urban...96" "Rural...97" "Total...98"
[99] "Urban...99" "Rural...100" "Total...101" "Urban...102" "Rural...103" "Total...104" "Urban...105"
[106] "Rural...106" "Total...107" "Urban...108" "Rural...109" "Total...110" "Urban...111" "Rural...112"
[113] "Total...113" "Urban...114" "Rural...115" "Total...116" "Urban...117" "Rural...118" "Total...119"
[120] "Urban...120" "Rural...121" "Total...122" "Urban...123" "Rural...124" "Total...125" "Urban...126"
[127] "Rural...127"

There is no column named Total or Total_1. There are many columns with names following the pattern Total...# where # is one to three digits. Do you want to select one of these, all of them, or a subset of them?

I only want to select a subset of them. I see now that I need to change the column names. Thank you so much!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.