2014-03-06 54 views
1

請原諒這個非常新手的問題,但我試圖在包含基於其他列的百分比的數據框中創建一個新列。例如,我正在使用的數據類似於以下內容,其中該列是一個二元因子(即存在或不存在「that」),動詞列是單個動詞(即動詞,可能或不可以在「that」之後),Freq列表示每個動詞的頻率。如何創建包含從其他列計算的百分比數據的新列?

 That Verb Freq 
1 That believe 3 
2 NoThat think 4 
3 That  say 3 
4 That believe 3 
5 That think 4 
6 NoThat  say 3 
7 NoThat believe 3 
8 NoThat think 4 
9 That  say 3 
10 NoThat think 4 

我想要的是添加另一列,爲每個不同的動詞提供「that」表達式(編碼爲「that」)的整體比率。類似如下:

 That Verb Freq Perc.That 
1 That believe 3  33.3 
2 NoThat think 4  25.0 
3 That  say 3  33.3 
4 That believe 3  33.3 
5 That think 4  25.0 
6 NoThat  say 3  33.3 
7 NoThat believe 3  33.3 
8 NoThat think 4  25.0 
9 That  say 3  33.3 
10 NoThat think 4  25.0 

這可能是我在其他地方錯過了一個類似的問題。如果是這樣,我表示歉意。不過,提前感謝任何幫助。

回答

0

您想使用的功能ddplyplyr庫:

#install.packages('plyr') 
library(plyr) 

dat # your data frame 

ddply(dat, .(verb), transform, perc.that = freq/sum(freq)) 

#  that verb freq perc.that 
#1 That believe 3 0.3333333 
#2 That believe 3 0.3333333 
#3 NoThat believe 3 0.3333333 
#4 That  say 3 0.3333333 
#... 
+0

這個工作勢如破竹。謝謝。 – user3388984