Mel
Mel

Reputation: 71

How to reference a column by its index and see if any of them have a certain value r

I have the following dataframe, and this is just a sample. I have 530 columns but I know the index of the columns that I need to check whether they contain certain values. I want to check whether column a, b, c, d contain the value 1. If they do then "YES", if not, then "NO". I ONLY want to reference the columns by their position not name.

df=data.frame(row_number=c(1,2,3,4),a=c(1,0,0,0),b=c(1,1,1,0),c=c(1,1,0,0),d=c(1,1,0,0))

I have tried this but it's not working, any suggestions?

df%>%mutate(check=ifelse(any(colnames(df[,c(2:5)])==1), "YES","NO"))

Upvotes: 0

Views: 138

Answers (2)

Ronak Shah
Ronak Shah

Reputation: 388817

You can use pmax to get maximum in each row and change 1 to 'Yes' and 0 to 'No'.

library(dplyr)
df %>% mutate(check = ifelse(do.call(pmax, select(.,2:5)), 'Yes','No'))

#  row_number a b c d check
#1          1 1 1 1 1   Yes
#2          2 0 1 1 1   Yes
#3          3 0 1 0 0   Yes
#4          4 0 0 0 0    No

Upvotes: 1

neilfws
neilfws

Reputation: 33772

Assuming that:

  • columns a, b, c and d contain only 1 or 0
  • you want to check if any column contains 1

then one approach is to check that the row sum over those columns is > 0.

library(dplyr)
df %>% 
  mutate(check = ifelse(rowSums(.[, 2:5]) > 0, "YES", "NO"))

Result:

  row_number a b c d check
1          1 1 1 1 1   YES
2          2 0 1 1 1   YES
3          3 0 1 0 0   YES
4          4 0 0 0 0    NO

Upvotes: 1

Related Questions