Trae
Trae

Reputation: 119

How to check a column against multiple variables in R

I am trying to find a way to test each unique identifier again multiple variables to find an out put. For example:

ID  Status       Ready
45   Ready       True
52   Ready       True
45   Not Ready   False
105  Not Ready   False
65   Ready       True

I want it to check each ID and for example if it checks ID:45 and anywhere in the status column it shows NOT READY to put False in all rows with ID:45

I am new to R and cant find anything that is even getting me close.

I apologize if this has been asked but i can't find it and wasn't sure how to word it.

Thank you for the help.

Upvotes: 1

Views: 104

Answers (2)

NelsonGon
NelsonGon

Reputation: 13309

Try this:

library(dplyr)
df %>% 
  mutate(NewID=ifelse(ID==45 & Status=="Not Ready",F,T))



#     ID    Status Ready NewID
   # 1  45     Ready  TRUE  TRUE
   # 2  52     Ready  TRUE  TRUE
   # 3  45 Not Ready FALSE FALSE
   # 4 105 Not Ready FALSE  TRUE
   # 5  65     Ready  TRUE  TRUE

Upvotes: 0

tofd
tofd

Reputation: 620

You mean something like this?


foo.df <- data.frame(ID = c(45,52,45,105,65),
                     Status = c('Ready', 'Ready', 'Not Ready',
                                'Not Ready', 'Ready'),
                     Ready = c(TRUE, TRUE, FALSE, FALSE, TRUE))

foo.df$IDReady <- sapply(foo.df$ID, function(id){
  sum(foo.df[foo.df$ID == id, 'Status'] == 'Not Ready') == 0
})

Upvotes: 1

Related Questions