robbie robbie - 20 days ago 7
R Question

R find value in multiple data frame columns

Given a data set where a value could be in any of a set of columns from the dataframe:

df <- data.frame(h1=c('a', 'b', 'c', 'a', 'a', 'b', 'c'), h2=c('b', 'c', 'd', 'b', 'c', 'd', 'b'), h3=c('c', 'd', 'e', 'e', 'e', 'd', 'c'))


How can I get a logical vector that specifies which rows contain the target value? In this case, searching for 'b', I'd want a logical vector with rows (1,2,4,6,7) as TRUE.

The real data set is much larger and more complicated so I'm trying to avoid a for loop.

thanks

EDIT:

This seems to work.

>apply(df, 1, function(x) {'b' %in% as.vector(t(x))}) -> i
> i
[1] TRUE TRUE FALSE TRUE FALSE TRUE TRUE

Answer

If speed is a concern I would go with:

rowSums(df == "b") > 0
Comments