Meijie Li
Meijie Li

Reputation: 1

error of unused argument(T) in ifelse in R

I try to reverse the boolean vector with ifelse:

labels <- disease%>% 
  select(humansAffected)%>%
  is.na()%>%
  ifelse(T, F,T)

However, I got an error: Error in ifelse(., labels == T, F, T) : unused argument (T) even if I changed to :

labels <- disease%>% 
    select(humansAffected)%>%
    is.na()%>%
    ifelse(labels==T,"FALSE","TRUE")

it shows error: Error in ifelse(., labels == T, "FALSE", "TRUE") : unused argument ("TRUE").

But when I write like this, I got the result I want:

labels <- disease%>% 
  select(humansAffected)%>%
  is.na()
ifelse(labels==T,F,T)

I want to know why I can't get the answer when I write them together by using %>%?

Upvotes: 0

Views: 807

Answers (2)

Jay Achar
Jay Achar

Reputation: 1271

Without a sample of your data, it's difficult to understand exactly what you need, but I hope this example will point you in the right direction.

library(dplyr)
disease %>% 
    select(humansAffected) %>%
    filter(!is.na(humansAffected)) %>%
    mutate(humansAffected = ifelse(humansAffected == TRUE, FALSE, TRUE))

Upvotes: 1

Ronak Shah
Ronak Shah

Reputation: 388982

If you want only one column in the final dataframe use transmute. You can negate is.na to get FALSE for NA and TRUE for non-NA.

library(dplyr)
disease <- disease %>% transmute(new_col = !is.na(humansAffected))

In base R, you can do :

disease <- transform(disease, new_col = !is.na(humansAffected))

and then select the columns that you want.

Upvotes: 0

Related Questions