I have a data frame with data similar to this: NameA GrpA NameB GrpB Dist A Alpha B Alpha 0.2 A Alpha C Beta 0.2 A Alpha D Beta 0.4 B Alpha C Beta 0.2 B Alpha D Beta 0.1 C Beta D Beta 0.3 Dist is a distance measure between two entities. The table displays all to all distances, but the distance between two entities only appears once. What I would like to get is a table where I get a count of entities per group where the distance satisfies a certain condition ( equal to zero for instance). In this case, if the requirement was Distances == 0.2 Alpha Beta Alpha 1 2 Beta 2 0 This resulting table would be symmetrical. I hope I am able to convey what I would like, and TIA for your help! Karin -- Karin Lagesen, PhD student karin.lagesen at medisin.uio.no http://folk.uio.no/karinlag
Try this: # read test data Lines <- "NameA GrpA NameB GrpB Dist A Alpha B Alpha 0.2 A Alpha C Beta 0.2 A Alpha D Beta 0.4 B Alpha C Beta 0.2 B Alpha D Beta 0.1 C Beta D Beta 0.3 " DF <- read.table(textConnection(Lines), header = TRUE) # process x <- xtabs(~ GrpA + GrpB, DF, Dist == 0.2) x + t(x) - diag(diag(x)) On Feb 18, 2008 10:16 AM, Karin Lagesen <karin.lagesen at medisin.uio.no> wrote:> > I have a data frame with data similar to this: > > NameA GrpA NameB GrpB Dist > A Alpha B Alpha 0.2 > A Alpha C Beta 0.2 > A Alpha D Beta 0.4 > B Alpha C Beta 0.2 > B Alpha D Beta 0.1 > C Beta D Beta 0.3 > > Dist is a distance measure between two entities. The table displays > all to all distances, but the distance between two entities only > appears once. What I would like to get is a table where I get a count > of entities per group where the distance satisfies a certain condition > ( equal to zero for instance). > > In this case, if the requirement was Distances == 0.2 > > Alpha Beta > Alpha 1 2 > Beta 2 0 > > This resulting table would be symmetrical. > > I hope I am able to convey what I would like, and TIA for your help! > > Karin > -- > Karin Lagesen, PhD student > karin.lagesen at medisin.uio.no > http://folk.uio.no/karinlag > > ______________________________________________ > 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. >
Try this: with(x, {tmp <- table(x[Dist==0.2,c('GrpA', 'GrpB')]) tmp[lower.tri(tmp)] <- tmp[upper.tri(tmp)] tmp}) On 18/02/2008, Karin Lagesen <karin.lagesen at medisin.uio.no> wrote:> > I have a data frame with data similar to this: > > NameA GrpA NameB GrpB Dist > A Alpha B Alpha 0.2 > A Alpha C Beta 0.2 > A Alpha D Beta 0.4 > B Alpha C Beta 0.2 > B Alpha D Beta 0.1 > C Beta D Beta 0.3 > > Dist is a distance measure between two entities. The table displays > all to all distances, but the distance between two entities only > appears once. What I would like to get is a table where I get a count > of entities per group where the distance satisfies a certain condition > ( equal to zero for instance). > > In this case, if the requirement was Distances == 0.2 > > Alpha Beta > Alpha 1 2 > Beta 2 0 > > This resulting table would be symmetrical. > > I hope I am able to convey what I would like, and TIA for your help! > > Karin > -- > Karin Lagesen, PhD student > karin.lagesen at medisin.uio.no > http://folk.uio.no/karinlag > > ______________________________________________ > 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
If d is your dataframe, how about ind <- d$Dist == 2 aggregate(d,by=list(d$NameA,dNameA),FUN=length) Regards, Moshe. --- Karin Lagesen <karin.lagesen at medisin.uio.no> wrote:> > I have a data frame with data similar to this: > > NameA GrpA NameB GrpB Dist > A Alpha B Alpha 0.2 > A Alpha C Beta 0.2 > A Alpha D Beta 0.4 > B Alpha C Beta 0.2 > B Alpha D Beta 0.1 > C Beta D Beta 0.3 > > Dist is a distance measure between two entities. The > table displays > all to all distances, but the distance between two > entities only > appears once. What I would like to get is a table > where I get a count > of entities per group where the distance satisfies a > certain condition > ( equal to zero for instance). > > In this case, if the requirement was Distances => 0.2 > > Alpha Beta > Alpha 1 2 > Beta 2 0 > > This resulting table would be symmetrical. > > I hope I am able to convey what I would like, and > TIA for your help! > > Karin > -- > Karin Lagesen, PhD student > karin.lagesen at medisin.uio.no > http://folk.uio.no/karinlag > > ______________________________________________ > 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. >