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]]
Hello, Your dataset seems to have a problem, the first row should the columns' names. If I'm right, nms <- levels(dat[[1]])[dat[1,1]] nms[2] <- levels(dat[[2]])[dat[1,2]] dat <- dat[-1, ] names(dat) <- nms with(dat, table(Env, place)) If I'm wrong, with(dat, table(V1, V2)) Hope this helps, Rui Barradas Em 08-10-2012 13:42, Nico Met escreveu:> 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.
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.
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.