[R] How to count combinations

Rui Barradas ruipbarradas at sapo.pt
Mon Oct 8 15:10:02 CEST 2012


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.




More information about the R-help mailing list