Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Data Frame summary showing factor frequency and total percentage

Tags:

r

I have an R data frame "internet" containing the following data:

> internet
   Time       Domain    Category File_Type
1   1.9   google.com  News_Media      html
2   2.0   google.com  News_Media       css
3   2.0   google.com  News_Media       jpg
4   2.1   google.com  News_Media       jpg
5   4.0 facebook.com      Social      html
6   4.0 facebook.com      Social       jpg
7   4.1 facebook.com      Social       css
8   5.9    bbc.co.uk  News_Media      html
9   6.0    bbc.co.uk  News_Media       css
10  6.0    bbc.co.uk  News_Media       jpg
11  6.1    bbc.co.uk  News_Media       jpg
12  6.0 facebook.com      Social      html
13  6.0 facebook.com      Social       jpg
14  6.1 facebook.com      Social       css

I'd like to create a summary of this data like as follows, though I am struggling to find the right way to do this.


Domain         Frequency   Total Percentage
bbc.co.uk      4           28.57143
facebook.com   6           42.85714
google.com     4           28.57143

I am able to produce a factor of Domains using:

domains <- tapply(internet$Domain, internet$Domain, FUN=length)

and a factor of totalPercentage using:

totalPercentage <- (domain/nrow(internet))*100

but I'm not quite sure how to join these into one summary.

Thank you all in advance for your time and help

like image 394
Andy Avatar asked Nov 18 '13 04:11

Andy


2 Answers

What about this:

multi.fun <- function(x) {cbind(freq = table(x), percentage = prop.table(table(x))*100)}
multi.fun(proxy$Domain)
like image 154
martin Avatar answered Nov 14 '22 17:11

martin


If you instal the epicalc package, you can get this with ?codebook. Consider:

proxy <- read.table(text="Row   Time       Domain    Category File_Type
1   1.9   google.com  News_Media      html
2   2.0   google.com  News_Media       css
3   2.0   google.com  News_Media       jpg
4   2.1   google.com  News_Media       jpg
5   4.0 facebook.com      Social      html
6   4.0 facebook.com      Social       jpg
7   4.1 facebook.com      Social       css
8   5.9    bbc.co.uk  News_Media      html
9   6.0    bbc.co.uk  News_Media       css
10  6.0    bbc.co.uk  News_Media       jpg
11  6.1    bbc.co.uk  News_Media       jpg
12  6.0 facebook.com      Social      html
13  6.0 facebook.com      Social       jpg
14  6.1 facebook.com      Social       css", header=T)

library(epicalc)
codebook(proxy)
... (output skipped)
================== 
Domain    :       
             Frequency Percent
bbc.co.uk            4    28.6
facebook.com         6    42.9
google.com           4    28.6
... (output skipped)
like image 2
gung - Reinstate Monica Avatar answered Nov 14 '22 16:11

gung - Reinstate Monica