[R] How to Group Categorical data in R?

Jorge I Velez
Fri, 16 Mar 2012 12:57:00 -0000

Hi Manish,

Try

# data set
x <- structure(list(V1 = structure(c(1L, 3L, 4L, 2L, 4L, 2L), .Label
c("A",
"E", "X", "Z"), class = "factor"), V2 =
structure(c(1L, 2L, 3L,
4L, 3L, 4L), .Label = c("B", "T", "U",
"V"), class = "factor"),
    V3 = structure(c(1L, 1L, 3L, 3L, 3L, 2L), .Label = c("C",
    "P", "Z"), class = "factor"), V4 =
structure(c(1L, 2L, 3L,
    4L, 3L, 4L), .Label = c("D", "K", "M",
"R"), class = "factor")), .Names
= c("V1",
"V2", "V3", "V4"), class = "data.frame",
row.names = c(NA, -6L
))

# result
lapply(split(x, x$V3), "[", c(1, 2, 4))

See ?lapply, ?split and ?"[" for more information.

HTH,
Jorge.-


On Fri, Mar 16, 2012 at 4:43 AM, Manish Gupta <> wrote:

> Hi,
>
> I am newbie to R and working on result presentation? My Input table is in
> following format
>
> A               B           C            D
> X               T            C            K
> Z               U           Z            M
> E               V            Z            R
> Z               U           Z            M
> E               V            P            R
>
> I need to present my result in the following way.
>
> Colum3 C
> A            B            D
> X            T             K
>
> Column3 Z
> Z               U              M
> E               V                R
> Z               U              M
>
> Column3 P
> E               V            R
>
> How can i implement it?
>
> Thanks
>
> --
> View this message in context:
>
http://r.789695.n4.nabble.com/How-to-Group-Categorical-data-in-R-tp4477622p4477622.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help@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.
>

	[[alternative HTML version deleted]]