I have a dataframe df_3 from which I want to mutate multiple columns starting with Team_. I want to replace 0s contained in the columns with NA. I use a code which I have previously successfully used but now gives me the following error:
Error in `mutate()`:
ℹ In argument: `across(starts_with("Team_"), ~na_if(., "0"))`.
Caused by error in `across()`:
! Can't compute column `Team_Num_1`.
Caused by error in `na_if()`:
! Can't convert `y` <character> to match type of `x` <double>.
Backtrace:
1. df_3 %>% mutate(across(starts_with("Team_"), ~na_if(., "0")))
10. dplyr::na_if(Team_Num_1, "0")
Any idea why that is or how I can solve it? I did not change anything in the original df and the code used to run through before hand, not sure what has changed.
Replicable code:
structure(list(Team_1 = c("0", "werg", "sdf"), Team_Desc_1 = c("wer",
"wtrb", "wergt"), Team_URL_1 = c("ewrg", "werg", "asd"), Team_Ver_1 = c("25",
"2523", "342"), Team_Num_1 = c(0, 23, 12), Team_Value_1 = c("aed",
"jfsa", "vsf"), Name_1 = c("etwbv", "werg", "sdfg"), Txt_1 = c("abc",
"bfh", "fse"), Head_1 = c("abc1", "bfh", "fse"), Team_2 = c("werh",
"wtt", "qwe"), Team_Desc_2 = c("sdfg", "wer", "sdfgv"), Team_URL_2 = c("qwe",
"gvre", "vrw"), Team_Ver_2 = c("4123", "5133", "4126"), Team_Num_2 = c(3,
0, 123), Team_Value_2 = c("aewed", "jfsbwa", "vsbf"), Name_2 = c("qwreg",
"gvr", "wref"), Txt_2 = c("rege", "wer", "vwr"), Head_2 = c("rege1",
"wer", "vwr")), row.names = c(NA, -3L), class = c("tbl_df", "tbl",
"data.frame"))