Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R find value in multiple data frame columns

Tags:

dataframe

r

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
like image 525
robbie Avatar asked Dec 11 '22 14:12

robbie


2 Answers

If speed is a concern I would go with:

rowSums(df == "b") > 0
like image 103
flodel Avatar answered Jan 03 '23 14:01

flodel


apply(df, 1, function(r) any(r == "b"))
like image 27
Hong Ooi Avatar answered Jan 03 '23 13:01

Hong Ooi