1

I have been using this in order to get the proportion of counts for each class for two groups but am not getting the right results.

It must be the proportion of vegclas within each density and not the overall proportion of the total count.

veg %>%
  group_by(density,vegclas) %>%
  summarise(total.count=n(),
                 avg=total.count/count(veg)*100)

Result:

Source: local data frame [12 x 4]
Groups: density [?]

   density vegclas total.count      avg
    (fctr)  (fctr)       (int)    (chr)
1        H       1        1201 <dbl[1]>
2        H       2        1258 <dbl[1]>
3        H       3         162 <dbl[1]>
4        H       4        4745 <dbl[1]>
5        H       5        2807 <dbl[1]>
6        H       6         114 <dbl[1]>
7        L       1        1109 <dbl[1]>
8        L       2         469 <dbl[1]>
9        L       3          56 <dbl[1]>
10       L       4        1701 <dbl[1]>
11       L       5         691 <dbl[1]>
12       L       6         204 <dbl[1]>

Edit:

Using the length(veg) gave my this:

   density vegclas total.count      avg
    (fctr)  (fctr)       (int)    (dbl)
1        H       1        1201  7506.25
2        H       2        1258  7862.50
3        H       3         162  1012.50
4        H       4        4745 29656.25
5        H       5        2807 17543.75
6        H       6         114   712.50
7        L       1        1109  6931.25
8        L       2         469  2931.25
9        L       3          56   350.00
10       L       4        1701 10631.25
11       L       5         691  4318.75
12       L       6         204  1275.00
user3655531
  • 145
  • 1
  • 10
  • I'd guess it get messed up because of the `count` function. Maybe try `length(veg)` instead. Either way, we can't really reproduce this. – David Arenburg Nov 18 '15 at 08:59
  • 1
    [How to make a great R reproducible example?](http://stackoverflow.com/questions/5963269) – zx8754 Nov 18 '15 at 08:59
  • If I understand correcly what you want: As `veg` is a dataframe (or a similar 2D object) try `nrow` instead of `length`or `count` – Mark Heckmann Nov 18 '15 at 09:11
  • Very close with the nrow solution. I gives me the proportion of all the rows and not within each density... – user3655531 Nov 18 '15 at 09:20

1 Answers1

0

As you lack a reproducible example I use the mtcars dataset. To get the percentages for each level of cyl (vegclas in your case) within each level of gear (density in your case) you may do:

mtcars %>% 
  group_by(gear) %>% 
    mutate(
       n.gear=n()
    ) %>%
  group_by(gear, cyl) %>% 
    summarise(
       n = n(),
       percent = n / unique(n.gear) * 100
    )

Note that the percentages add up to 100 in each level of gear.

   gear   cyl     n   percent
  (dbl) (dbl) (int)     (dbl)
1     3     4     1  6.666667
2     3     6     2 13.333333
3     3     8    12 80.000000
4     4     4     8 66.666667
5     4     6     4 33.333333
6     5     4     2 40.000000
7     5     6     1 20.000000
8     5     8     2 40.000000
Community
  • 1
  • 1
Mark Heckmann
  • 10,943
  • 4
  • 56
  • 88