[R-sig-teaching] Creating table from data.frame

Philippe Grosjean phgrosjean at sciviews.org
Wed May 5 21:37:03 CEST 2010


This is easy:

# Recode gender
levels(x$gender) <- c("men", "women")
# Build the two-way table (and print it)
(x.Table <- xtabs(count ~ status + gender, data = x))

Best,

PhG

..............................................<°}))><........
  ) ) ) ) )
( ( ( ( (    Prof. Philippe Grosjean
  ) ) ) ) )
( ( ( ( (    Numerical Ecology of Aquatic Systems
  ) ) ) ) )   Mons University, Belgium
( ( ( ( (
..............................................................

On 05/05/10 17:30, Byungchul Cha wrote:
>
> I am a relatively novice in R and I need some help.
>
> I want to create a two-way table from a data given as a data.frame, which looks like this:
>
>> x
>     group status gender count
> 1      1     s1     g1   890
> 2      2     s1     g2   969
> 3      3     s2     g1   340
> 4      4     s2     g2   403
> 5      5     s3     g1  2897
> 6      6     s3     g2  3321
> 7      7     s4     g1   249
> 8      8     s4     g2   383
> 9      9     s5     g1   306
> 10    10     s5     g2   366
> 11    11     s6     g1   160
> 12    12     s6     g2   137
>
> I would like a two way table, which would look like
>
> status    men    women
> s1    890    969
> s2    340    403
> s3    2897    3321
> ...
>
> How do I do this in a relatively efficient way? Thanks.
>
> Byungchul
>
> _______________________________________________
> R-sig-teaching at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-teaching
>
>




More information about the R-sig-teaching mailing list