I am working with a dataset called fab_2023
, which I imported from Excel. Among the variables in the dataset, I need to convert certain ones into numeric format. However, these variables do contain missing values (NAs). Additionally, the original numeric values have more than two decimal places, and my goal is to round these numerical values to precisely two decimal places. This is what I did:
fab_2023 <- fab_2023 %>%
mutate_at(vars(Total_score_2023:MA_Combined_Social_2021), ~ ifelse(is.na(.), NA, as.numeric(.)))
fab_2023 <- fab_2023 %>%
mutate_at(vars(Total_score_2023:MA_Combined_Social_2021), ~ ifelse(is.na(.), NA, round(., 2)))
I basically want to convert all variables from Total_score_2023 to MA_Combined_Social variables to numerical variables and round up to two decimal places. When I do that, it seems it actually does convert the data in the way I want but then I encounter the following error message:
Error in `mutate()`:
! Problem while computing `Total_score_2021 = (structure(function (..., .x = ..1, .y = ..2, . = ..1) ...`.
Caused by error in `round()`:
! non-numeric argument to mathematical function
Run `rlang::last_error()` to see where the error occurred.
Can you please advise me if I am doing something wrong? Thank you!