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

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -