Skip to content

Analogy for %in% for the whole columns (rather than individual values)

4 messages · Jorge Ivan Velez, Fredrik Karlsson, Dimitri Liakhovitski

#
Hello!

I have a matrix a with 2 variables (see below) that contain character strings.
I need to create a 3rd variable that contains True if the value in
column x is equal to the value in column y. The code below does it.

a<-data.frame(x=c("john", "mary", "mary",
"john"),y=c("mary","mary","john","john"))
a$x<-as.character(a$x)
a$y<-as.character(a$y)
a$equal<-NA
for(i in 1:nrow(a)) {a[i,3]<-a[i,1] %in% a[i,2]}

I was wondering if one can do it faster, without doing it row-by-row
as I did it. Maybe there is a faster way to compare 2 columns?

Thank you very much!
#
Thank you very much! I tried %in% and it did not work. However == works!
Thank you!
On Thu, Mar 26, 2009 at 3:05 PM, Fredrik Karlsson <dargosch at gmail.com> wrote: