[R] tabulation on dataframe question

Gabor Grothendieck ggrothendieck at gmail.com
Mon Feb 18 16:56:27 CET 2008


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.
>



More information about the R-help mailing list