[R] Pivot Table "like" structure
John Kane
jrkrideau at inbox.com
Tue Oct 16 17:14:06 CEST 2012
Perhaps this but your results example did not include Char1.
ibrary(reshape2)
md <- structure(list(Coutry = structure(c(3L, 3L, 3L, 3L, 2L, 2L, 1L,
1L), .Label = c("J", "M", "U"), class = "factor"), State = structure(c(1L,
1L, 4L, 2L, 5L, 5L, 3L, 6L), .Label = c("A", "C", "K", "O", "S",
"T"), class = "factor"), City = structure(c(1L, 8L, 7L, 2L, 3L,
6L, 5L, 4L), .Label = c("BEN", "HRD", "JKL", "KK", "KL", "KMM",
"OKC", "TYU"), class = "factor"), Char1 = structure(c(1L, 2L,
1L, 3L, 4L, 2L, 3L, 5L), .Label = c("A", "B", "C", "D", "M"), class = "factor"),
Char2 = structure(c(1L, 2L, 1L, 2L, 3L, 4L, 4L, 2L), .Label = c("ABCD",
"EFGH", "FGHJ", "GGGG"), class = "factor"), Char3 = structure(c(1L,
1L, 2L, 3L, 1L, 1L, 2L, 3L), .Label = c("ASDFG", "DDDDD",
"EEEEEE"), class = "factor")), .Names = c("Coutry", "State",
"City", "Char1", "Char2", "Char3"), row.names = c(NA, -8L), class = "data.frame")
head(md)
str(md)
md1 <- melt(md, id = c("Coutry", "State", "City"))
apply(md1, 2, count)
John Kane
Kingston ON Canada
> -----Original Message-----
> From: vickythakre at gmail.com
> Sent: Sat, 13 Oct 2012 19:38:30 -0500
> To: r-help at r-project.org
> Subject: [R] Pivot Table "like" structure
>
> HI Team,
>
> I am currently working on problem and stumped on "for" loop.
>
> Data:
>
> structure(list(Coutry = structure(c(3L, 3L, 3L, 3L, 2L, 2L, 1L,
> 1L), .Label = c("J", "M", "U"), class = "factor"), State =
> structure(c(1L,
> 1L, 4L, 2L, 5L, 5L, 3L, 6L), .Label = c("A", "C", "K", "O", "S",
> "T"), class = "factor"), City = structure(c(1L, 8L, 7L, 2L, 3L,
> 6L, 5L, 4L), .Label = c("BEN", "HRD", "JKL", "KK", "KL", "KMM",
> "OKC", "TYU"), class = "factor"), Char1 = structure(c(1L, 2L,
> 1L, 3L, 4L, 2L, 3L, 5L), .Label = c("A", "B", "C", "D", "M"), class =
> "factor"),
> Char2 = structure(c(1L, 2L, 1L, 2L, 3L, 4L, 4L, 2L), .Label =
> c("ABCD",
> "EFGH", "FGHJ", "GGGG"), class = "factor"), Char3 = structure(c(1L,
> 1L, 2L, 3L, 1L, 1L, 2L, 3L), .Label = c("ASDFG", "DDDDD",
> "EEEEEE"), class = "factor")), .Names = c("Coutry", "State",
> "City", "Char1", "Char2", "Char3"), row.names = c(NA, -8L), class =
> "data.frame")
>
> Question:
>
> I am trying to create a pivot table which will count the occurrences of
> Char1 : Char4
> from the columns Coutry, State, City. I am not sure to use all the four
> columns and get something like
>
> structure(list(Group.1 = structure(1:4, .Label = c("ABCD", "EFGH",
> "FGHJ", "GGGG"), class = "factor"), x = c(2L, 3L, 1L, 2L)), .Names =
> c("Group.1",
> "x"), row.names = c(NA, -4L), class = "data.frame")
>
> Code which I tried to use with not best results:
>
> aggregate(State, list(Char2), FUN="count")
>
>
> Best Regards,
>
>
> Bhupendrasinh Thakre
>
>
>
>
>
> [[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.
____________________________________________________________
GET FREE SMILEYS FOR YOUR IM & EMAIL - Learn more at http://www.inbox.com/smileys
Works with AIM®, MSN® Messenger, Yahoo!® Messenger, ICQ®, Google Talk™ and most webmails
More information about the R-help
mailing list