can't transform a data frame with duplicate names mutate() function in dplyr

Viewed 51

Yesterday when I tried to use mutate() function to recode some values in a specific column, everything was fine. But suddenly, I run these codes again today, it returns errors from the line with mutate() function:

 Can't transform a data frame with duplicate names.

Backtrace:

  1. EVS_recode %>% mutate_at(c("C001"), ~na_if(., -1))
  2. dplyr::mutate_at(., c("C001"), ~na_if(., -1))
  3. dplyr:::mutate.data.frame(.tbl, !!!funs)
  4. dplyr:::mutate_cols(.data, ..., caller_env = caller_env())
  5. DataMask$new(.data, caller_env)
  6. dplyr:::initialize(...)

here are the codes I used which work perfectly yesterday

#recode the 1 in column C001 to be 5, 2 to be 4#

EVS_recode <- EVS_recode %>% mutate(C001= recode(C001, '1' = 5, '2'= 4))

#recode the -1,-2,-4,-5 values to be NA in column C001#

EVS_recode <- EVS_recode %>% mutate_at(c('C001'), ~na_if(.,-1))

EVS_recode <- EVS_recode %>% mutate_at(c('C001'), ~na_if(.,-2))

EVS_recode <- EVS_recode %>% mutate_at(c('C001'), ~na_if(.,-4))

EVS_recode <- EVS_recode %>% mutate_at(c('C001'), ~na_if(.,-5))

This is my first time to have such large scale of codes not working related to mutate(). Could someone help me or has the similar problem when using mutate() ?

Thanks for your time to read this! It would be very helpful if someone can give me a hint.

1 Answers

You cannot have two variables with the same name in your dataframe, hence the error.

You could try using case_when() to achieve this in one go and maybe it resolves the error

library(dplyr)
df <- tibble::tibble(C001 = c(-1,1,-2,3, -4, -5, 5,5,3,2,1,-1))
df %>% 
  mutate(across(C001, ~ case_when(
    C001 == 1 ~ 5,
    C001 == 2 ~ 4,
    C001 %in% c(-1, -2, -4,-5) ~ NA_real_, 
    TRUE ~ C001
  )))

You could also use replace

df %>% mutate(across(C001, ~replace(., . %in% c(-1, -2, -4,-5), NA)))
Related