*Hi all, * *Assume that I have the following data set with tow variables and I want count the number of observation with identical values * ** *x1 x2* * 1 1 * * 1 0 * * 0 1* * 0 1* * 0 0* * 1 1* * 0 1 * I want the following output ** * * *n1=3 # number of identical observation between x1 and x2 variables* *n2=4 # number of different observation* How do I do it in R? Thanks a lot ** [[alternative HTML version deleted]]
Try this: table(Reduce(`==`, DF)) On Tue, Oct 13, 2009 at 9:20 AM, Ashta <sewashm at gmail.com> wrote:> *Hi all, > * > > *Assume that I have the following data set ?with tow variables and I want > count the number of observation with identical values > * > > ** > > *x1 x2* > > * 1 ? 1 * > > * 1 ? 0 * > > * 0 ? 1* > > * 0 ? 1* > > * 0 ? 0* > > * 1 ? 1* > > * 0 ? 1 > * > > > I want the ?following output > ** > > * > * > > *n1=3 ?# number of identical observation between x1 and x2 variables* > > *n2=4 ?# number of different observation* > > > How do I do it in R? > > > Thanks a lot > > > > > ** > > ? ? ? ?[[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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Ashta > Sent: Tuesday, October 13, 2009 5:20 AM > To: R help > Subject: [R] Counting > > *Hi all, > * > > *Assume that I have the following data set with tow > variables and I want > count the number of observation with identical values > * > > ** > > *x1 x2* > > * 1 1 * > > * 1 0 * > > * 0 1* > > * 0 1* > > * 0 0* > > * 1 1* > > * 0 1 > * > > > I want the following output > ** > > * > * > > *n1=3 # number of identical observation between x1 and x2 variables* > > *n2=4 # number of different observation*sum() converts TRUE to 1 and FALSE to 0 so the following works n1 <- sum(x1 == x2) n2 <- sum(x1 != x2) You can also use table() to get both numbers in one vector. In the following I make table's input a factor (a) to make sure that both the == and != counts are in the table even if one count is zero and (b) to put them in the order you asked for, TRUE then FALSE: n12 <- table(factor(x1==x2, levels=c(TRUE,FALSE))) n1 <- n12[1] n2 <- n12[2] If there may be missing values in the data then you have to decide how to handle them. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> > > How do I do it in R? > > > Thanks a lot > > > > > ** > > [[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. >
Hi All, Assume that I have the following data set? with two variables and I want count the number of observation with identical values ?and number of time each factor changed from x1 to x2. x1 ?x2 ?1?? ?1 ?1?? ?0 ?0?? ?1 ?0?? ?1 ?0?? ?0 ?1?? ?1 0??? 1 The output should be x1 ?changed 0 ??3 ???# has changed 3 times 1? ?1??? # has changed 1 time x1 unchanged ??????? ????0? 1 ???# has unchanged only 1 time 1 ?2? ???# has unchanged 2 times Can someone help me how to do it in R? Thanks in advance
How about unch <- aggregate(x2==x1, by = list(x1=x1), FUN = sum) chgd <- aggregate(x2!=x1, by = list(x1=x1), FUN = sum) -Peter Ehlers Ashta wrote:> Hi All, > > Assume that I have the following data set with two variables and I > want count the number of observation with identical values and number > of time each factor changed from x1 to x2. > > x1 x2 > 1 1 > 1 0 > 0 1 > 0 1 > 0 0 > 1 1 > 0 1 > > The output should be > x1 changed > 0 3 # has changed 3 times > 1 1 # has changed 1 time > x1 unchanged > 0 1 # has unchanged only 1 time > 1 2 # has unchanged 2 times > > Can someone help me how to do it in R? > > Thanks in advance > > ______________________________________________ > 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. > >
Seemingly Similar Threads
- Fwd: Bad \usage lines question
- Fastest way to do HWE.exact test on 100K SNP data?
- Convertir programa Matlab a R sacado de Threshold Models of Collective Behavior de Michèle Lai & Yann Poltera
- how to create data.frame with dynamic count of values
- same test statistic for t-test with and without equal variance assumption