[R] How to count combinations
arun
smartpink111 at yahoo.com
Mon Oct 8 15:13:36 CEST 2012
Hi,
Not clear how you want your output in matrix:
Try this:
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)
list1<-lapply(lapply(split(dat1,dat1$place),`[`,1),table)
mat1<-matrix(unlist(list1),ncol=3)
names(mat1)<-names(unlist(list1))
mat1
# [,1] [,2] [,3]
#[1,] 1 1 3
#[2,] 1 2 1
#[3,] 1 1 3
#[4,] 1 2 4
#attr(,"names")
#[1] "Ck.23" "Ck.67" "CK.23" "CK.67" "Dk.55" "IN.67" "jk.23" "NO.23" "NW.23"
#[10] "PK.55" "PR.67" "PT.23"
mat1["Ck.23"]
#Ck.23
# 1
mat1[mat1==3]
#NW.23 PR.67
# 3 3
Hope this helps.
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