Troubles with if_else -- Error: Problem with `mutate()` column

Hello, I am trying to make a new column in my data using mutate and if_else functions.

This is the code I am using:

    BWt <- BW %>%  
  mutate( PMday = if_else (Time_Weight = 2, true = Study_Day, false = 0))

And I obtaining this error message:

Error: Problem with mutate() column PMday.
i PMday = if_else(Time_Weight = 2, true = Study_Day, false = 0).
x unused argument (Time_Weight = 2)
Run rlang::last_error() to see where the error occurred.

The structure of the data is the following:

str(BW) # Check data structure
'data.frame': 144 obs. of 7 variables:
Date : chr "31-Mar-21" "31-Mar-21" "31-Mar-21" "31-Mar-21" ...
Study_Day : int 0 0 0 0 0 0 0 0 0 0 ...
Time_Weight : int 1 1 1 1 2 1 2 1 1 2 ...
Pen : int 1 1 1 5 5 1 1 1 5 5 ...
Animal_ID : int 2 8 3 4 4 7 7 6 9 9 ...
Treatment_Group: chr "A" "B" "E" "C" ...
Weight_kg : num 16.2 14.1 17.7 15 14.4 17.3 16.4 18.1 17.1 16.8 ...

I also have tried ifelse() but I get a similar error message:

Error: Problem with mutate() column PMday.
i PMday = ifelse(Time_Weight = 2, true = Study_Day, false = 0).
x unused arguments (Time_Weight = 2, true = Study_Day, false = 0)
Run rlang::last_error() to see where the error occurred.

Hope this information helps to get some advice. Let me know if you need to know anything else.
Many thanks!
Beatriz

Try

BWt <- BW %>%  
  mutate( PMday = if_else (Time_Weight == 2, true = Study_Day, false = 0))

Thanks, but I still get an error, although different this time.

Error in ifelse(Time_Weight == 2, true = Study_Day, false = 0) :
unused arguments (true = Study_Day, false = 0)

But just checked the table and actually worked!
Thanks :wink:

You've changed the function.

dplyr::if_else() does not have the same arguments as base::ifelse():

# check the arguments:
BWt <- BW %>% 
  mutate( PMday = if_else (Time_Weight == 2, true = Study_Day, false = 0))
BWt <- BW %>%  
  mutate( PMday = ifelse (Time_Weight == 2, yes = Study_Day, no = 0))

# no need to include the argument names:
BWt <- BW %>%  
  mutate( PMday = if_else (Time_Weight == 2, Study_Day, 0))
BWt <- BW %>%  
  mutate( PMday = ifelse (Time_Weight == 2, Study_Day, 0))

Thanks!

I deleted the table to see what worked and what not.

# With dplyr::if_else()
# Using double equal (==)
 BWt <- BW %>%  
 mutate( PMday = if_else (Time_Weight == 2, Study_Day, 0))

Error: false must be an integer vector, not a double vector.

# With dplyr::if_else()
# Using single equal (=)
BWt <- BW %>%  
mutate( PMday = if_else (Time_Weight = 2, Study_Day, 0))

Error in if_else(Time_Weight = 2, Study_Day, 0) :
unused argument (Time_Weight = 2)

# With base::ifelse()
# Using double equal (==)
BWt <- BW %>%  
mutate( PMday = ifelse (Time_Weight == 2, Study_Day,  0))

No issues!

The single = is not used for comparisons. Comparisons are always ==.

if_else() is stricter than ifelse(), so it's easier to use the latter until you know what's going on.

You probably need to coerce the values to integers to ensure the comparison works as intended: if_else (Time_Weight == 2L, Study_Day, 0L)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.