Skip to content

How to get the proportions of data with respect to two variables in R?

2 messages · umair durrani, jim holtman

#
Here is an example using data.table to get the proportion for the Length/Width:
+ 2 2 13.5 4.5
+ 2 2 13.5 4.5
+ 2 2 13.5 4.5
+ 2 2 13.5 4.5
+ 3 2 13.5 4.0
+ 3 2 13.5 4.0
+ 3 2 13.5 4.0
+ 3 2 13.5 4.0
+ 4 2 10.0 4.5
+ 4 2 10.0 4.5
+ 4 2 10.0 4.5
+ 4 2 10.0 4.5
+ 5 3 23.0 4.5
+ 5 3 23.0 4.5
+ 5 3 23.0 4.5
+ 5 3 23.0 4.5
+ 6 3 76.5 4.5
+ 6 3 76.5 4.5
+ 6 3 76.5 4.5
+ 6 3 76.5 4.5
+ 6 3 76.5 4.5
+ 7 1 10.0 3.0
+ 7 1 10.0 3.0
+ 7 1 10.0 3.0
+ 7 1 10.0 3.0
+ 8 2 13.5 5.5
+ 8 2 13.5 5.5
+ 8 2 13.5 5.5
+ 8 2 13.5 5.5", header = TRUE)
+     , list(count = .N)
+     , keyby = 'Class,Length,Width'
+     ]
+             , counts$Class
+             , FUN = function(x) round(x / sum(x) * 100, 1)
+             )
Class Length Width count prop
1:     1   10.0   3.0     1  100
2:     2   10.0   4.5     1   25
3:     2   13.5   4.0     1   25
4:     2   13.5   4.5     1   25
5:     2   13.5   5.5     1   25
6:     3   23.0   4.5     1   50
7:     3   76.5   4.5     1   50
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.
On Sun, Dec 1, 2013 at 3:05 AM, umair durrani <umairdurrani at outlook.com> wrote: