laksa February 2016

Is a value present in certain columns of a dataframe (by row)? Using %in% and | on multiple columns works, but is there a more concise way?

I have a dataframe that looks something like

> df = data.frame(c1=c(3,6,NA,5), c2=c(2,NA,3,NA), c3=c(1,4,4,2))
> df
  c1 c2 c3
1  3  2  1
2  6 NA  4
3 NA  3  4
4  5 NA  2

I would like to create additional columns that record, by each row, whether a given value is present in c1 or c2, ignoring c3.

This works but is not concise (especially with my actual data):

> df$is.2.in.c1.or.c2[df$c1 %in% 2 | df$c2 %in% 2] = 'It is there'
> df
  c1 c2 c3 is.2.in.c1.or.c2
1  3  2  1      It is there
2  6 NA  4             <NA>
3 NA  3  4             <NA>
4  5 NA  2             <NA>

Is there a more concise way?

Answers


akrun February 2016

We can try with rowSums after creating a logical matrix using ==.

i1 <- rowSums(df[1:2]==2, na.rm=TRUE)!=0

It is better to have a logical index ('i1') instead of flagging with 'It is there'. But, if needed,

ifelse(i1, 'It is there', NA)
#[1] "It is there" NA            NA            NA    

Post Status

Asked in February 2016
Viewed 2,894 times
Voted 6
Answered 1 times

Search




Leave an answer