0
的頻率我有一個DF:子集數據幀和應用功能計算每個因子水平
df<- data.frame(region= c("1", "1", "1","1","1","1","1","1", "2","2"),plot=c("1", "1", "1","2","2","2", "3","3","3","3"), interact=c("A_B", "C_D","C_D", "E_F","C_D","C_D", "D_E", "D_E","C_B","A_B"))
我想通過plot
到子集的數據。對於每個plot
子集,我想計算每個唯一的interact
類型的頻率。輸出應該是這樣的:
df<- data.frame(region= c("1", "1", "1","1", "2","2",
"2"),plot=c("1",
"1", "2","2", "3","3","3"), interact=c("A_B", "C_D", "E_F","C_D",
"D_E", "C_B","A_B"), freq= c(1,2,1,2,2,1,1))
然後我想使計算的DF的每個plot
子集以下功能:
sum<-sum(df$freq) # Calculate sum of `freq` for each plot subset (this calculates the total number of interactions)
prop<-unique(df$freq)/sum #Divide each level of `freq` by the sum (this finds the proportion of each interaction type to the total number of interactions)
prop2<-prop^2 # Square this proportion
D<-sum(prop2) # Find the sum of these proportion for each plot subset
simp<-1/D)# Use this to calculate simpsons diversity
我想使用的功能是相似在下頁解釋:http://rfunctions.blogspot.com.ng/2012/02/diversity-indices-simpsons-diversity.html。然而,引用的版本是在寬數據集上執行的,我的數據集將會很長。
最後,我將有值的每個情節DF:
result<-
Plot div
1 1.8
2 1.8
3 2.6
謝謝!這個解決方案非常有效。我對我的情節進行了編輯3.我對這個計算做了一個錯誤,你的回答是正確的。 – Danielle
此外,我想使用來自純素的多樣性(),但我的理解是它需要矩陣格式。您是否有一種有效的方法將多樣性()集成到子集上?再次感謝。 – Danielle
當然,這也有可能:)我通過使用'diversity()'函數添加方法來編輯我的帖子。看一看。 – MikolajM