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

Replace all occurrences of columns in a data frame

I have two columns Project ID and Project Number in my dataframe hpds_all_clean with NAs in them, and I’m trying to replace all NAs occurrences with "Undisclosed Project ID" and "Undisclosed Project Number". Here are some of my attempts which have the same error:

target of assignment expands to non-language object

Using gsub():

hpds_all_clean %>%
  filter(`Recipient Name-EN` == "Afghanistan") %>%
  hpds_all_clean$`Project Title - EN` = gsub("", "Undisclosed Project ID", hpds_all_clean$`Project Title - EN`) %>%
  hpds_all_clean$`Project Number/Numéro de projet` = gsub("", "Undisclosed Project Number", hpds_all_clean$`Project Number/Numéro de projet`)

Using paste0()

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

hpds_all_clean %>%
  filter(`Recipient Name-EN` == "Afghanistan") %>%
  paste0(hpds_all_clean, hpds_all_clean$`Project Title - EN`) = "Undisclosed Project ID" %>%
  paste0(hpds_all_clean, hpds_all_clean$`Project Number/Numéro de projet`) = "Undisclosed Project Number"

Any suggestion would be greatly appreciated. Thank you for your time!

>Solution :

If these values are NA you can do

hpds_all_clean %>% 
  mutate(`Project Title - EN` = if_else(is.na(`Project Title - EN`),"Undisclosed Project ID",`Project Title - EN`),
         `Project Number/Numéro de projet` = if_else(is.na(`Project Number/Numéro de projet`),"Undisclosed Project Number",`Project Number/Numéro de projet`))

If these "NA" values are actually "", you would edit within the above to: if_else(`Project Title - EN` == ""), etc

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