inti luna
2012-Aug-26 02:12 UTC
[R] Comparing two variables for match and creating a new vector with results
Hello, I need help comparing two variables and producing a third variable with the result.I have this data: id <-c(1,2,3,4,5,6,7,8,9,10) vi <-c(1,0,1,1,1,1,1,2,2,0) vi2 <-c(0,1,1,1,1,1,1,2,0,0) data<-cbind(id,vi,vi2) For the data in the example, I need to generate: id <-c(1,2,3,4,5,6,7,8,9,10) correct <-c(0,0,1,1,1,1,1,1,0,1) correct_data<-cbind(id,correct) Matrix "correct_data"is the result of comparing "vi" with "vi2" and see if they match. If the values are the same correct value=1 and if not correct value=0 keeping the same id position. How can I do this? I tried with match and with %in% but is not exactly what I need. Thanks in advance! Inti Luna [[alternative HTML version deleted]]
David Winsemius
2012-Aug-26 07:07 UTC
[R] Comparing two variables for match and creating a new vector with results
On Aug 25, 2012, at 7:12 PM, inti luna wrote:> Hello, > > I need help comparing two variables and producing a third variable > with the > result.I have this data: > > id <-c(1,2,3,4,5,6,7,8,9,10) > vi <-c(1,0,1,1,1,1,1,2,2,0) > vi2 <-c(0,1,1,1,1,1,1,2,0,0) > data<-cbind(id,vi,vi2) > > For the data in the example, I need to generate: > id <-c(1,2,3,4,5,6,7,8,9,10) > correct <-c(0,0,1,1,1,1,1,1,0,1) > correct_data<-cbind(id,correct)correct <- (vi == vi2) # although parens are superfluous The cbind() operation coerces the logical vector to numeric.> > Matrix "correct_data"is the result of comparing "vi" with "vi2" and > see if > they match. If the values are the same correct value=1 and if not > correct > value=0 keeping the same id position. > > How can I do this? I tried with match and with %in% but is not > exactly what > I need. > > Thanks in advance! > > Inti Luna > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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.David Winsemius, MD Alameda, CA, USA