r - Subset data in ddply -


i have following data , trying summarize have id, tone, , count of tone each name. however, want have tone rate (tone_cnt / sum of id name). unfortunately, i've messed around , have had little luck.

    df = data.frame(name=c("a","a","a","a","a","b","b","b","b","b","b"),                     id = c(35, 35, 35, 36, 36, 35, 35, 35, 36, 36, 36),                     tone=c(0,1,1,2,2,3,2,1,2,2,2))     df     sum = ddply(df, .(name, id, tone), summarise,  tone_cnt=length(tone), tone_rate=tone_cnt/sum(tone_cnt))     sum 

have messed around length(), sum(), sum(length()) , nothing seems trick done. currently, spits out 1 everyone.

here's how should like:

> sum   name id tone tone_cnt tone_rate 1    35    0        1        1 / (1+2) 2    35    1        2        2 / (1+2)  3    36    2        2        2 / (2) 4    b 35    1        1        1 / (1+1+1) 5    b 35    2        1        1 / (1+1+1) 6    b 35    3        1        1 / (1+1+1) 7    b 36    2        3        3 / (3) 

it's tone count divided sum of total count (name, id, tone)


Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -