[R] How to count combinations
arun
smartpink111 at yahoo.com
Mon Oct 8 15:21:38 CEST 2012
Hi,
Another way to get the matrix with combinations of two columns:
just use table()
dat1<-structure(list(Env = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("23",
"55", "67", "Env"), class = "factor"), place = structure(c(7L,
7L, 7L, 2L, 1L, 11L, 11L, 11L, 11L, 5L, 6L, 6L, 3L, 8L, 2L, 1L,
4L, 4L, 10L, 10L, 10L), .Label = c("Ck", "CK", "Dk", "IN", "jk",
"NO", "NW", "PK", "place", "PR", "PT"), class = "factor")), .Names = c("Env",
"place"), row.names = 2:22, class = "data.frame")
dat1[]<-sapply(dat1,as.character)
as.matrix(table(dat1$Env,dat1$place))
# Ck CK Dk IN jk NO NW PK PR PT
# 23 1 1 0 0 1 2 3 0 0 4
# 55 0 0 1 0 0 0 0 1 0 0
#67 1 1 0 2 0 0 0 0 3 0
A.K.
----- Original Message -----
From: Nico Met <nicomet80 at gmail.com>
To: R help <r-help at r-project.org>
Cc:
Sent: Monday, October 8, 2012 8:42 AM
Subject: [R] How to count combinations
Dear all,
Need a help. I would like to count combination of two columns:
structure(list(V1 = structure(c(4L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("23",
"55", "67", "Env"), class = "factor"), V2 = structure(c(9L, 7L,
7L, 7L, 2L, 1L, 11L, 11L, 11L, 11L, 5L, 6L, 6L, 3L, 8L, 2L, 1L,
4L, 4L, 10L, 10L, 10L), .Label = c("Ck", "CK", "Dk", "IN", "jk",
"NO", "NW", "PK", "place", "PR", "PT"), class = "factor")), .Names =
c("V1",
"V2"), class = "data.frame", row.names = c(NA, 22L))
For example: how many times 23 appear with NW and then I want to put the
out put as a matrix
NW Ck PT jK NO DK PK IN PR 23 3 55 67
Many thanks in advance
Nico
[[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.
More information about the R-help
mailing list