Dimitri Liakhovitski
2009-Mar-26 18:48 UTC
[R] Analogy for %in% for the whole columns (rather than individual values)
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! -- Dimitri Liakhovitski MarketTools, Inc. Dimitri.Liakhovitski at markettools.com
Jorge Ivan Velez
2009-Mar-26 18:58 UTC
[R] Analogy for %in% for the whole columns (rather than individual values)
Dear Dimitri: Try this: 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$TEST<-apply(a,1,function(x) x[1]==x[2]) a See ?apply for more details. HTH, Jorge On Thu, Mar 26, 2009 at 2:48 PM, Dimitri Liakhovitski <ld7631@gmail.com>wrote:> 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! > > -- > Dimitri Liakhovitski > MarketTools, Inc. > Dimitri.Liakhovitski@markettools.com > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Fredrik Karlsson
2009-Mar-26 19:05 UTC
[R] Analogy for %in% for the whole columns (rather than individual values)
Hi Dimitri, What about this:> with(a, x == y) -> a$equal > ax y equal 1 john mary FALSE 2 mary mary TRUE 3 mary john FALSE 4 john john TRUE /Fredrik On Thu, Mar 26, 2009 at 7:48 PM, Dimitri Liakhovitski <ld7631@gmail.com>wrote:> 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! > > -- > Dimitri Liakhovitski > MarketTools, Inc. > Dimitri.Liakhovitski@markettools.com > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- "Life is like a trumpet - if you don't put anything into it, you don't get anything out of it." [[alternative HTML version deleted]]