Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I specify the last column of a tibble within the rename_with() function's ".cols" argumnt in Dyplr?

Given a tibble "cars"

cars |>
  rename_with(~paste0(.x, ")"), .cols = (2:10))

appends a parenthesis to the names columns 2 to 10 of the tibble. How would one go about selecting from the second to the last column? I have accomplished it with

rename_with(~paste0(.x, ")"), .cols = (2:ncol(betterLifeIndicators)))

but I am sure there must be an elegant way to do it within the dyplr package. I was also wondering if there is a way to select all columns except the first (or any other specified set).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thank you in advance!

>Solution :

Let me know if this works.

# Last column
cars |> 
  rename_with(~paste0(.x, ")"), .cols = last_col())

# 2nd to Last column
cars |> 
  rename_with(~paste0(.x, ")"), .cols = 2:last_col())

# All but first (same as above)
cars |> 
  rename_with(~paste0(.x, ")"), .cols = -1)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading