如果我的問題是正確的,您希望看到變量中的值的頻率,該變量是Clade
列中的值以及單元中此類變量的每個值。
我會做重複的例子,所以你可以調整到您的特定需求:
library(dplyr)
set.seed(1)
values <- c('one', 'two', 'three', 'four', 'five')
df <- data.frame(clade =sample(values, size = 1000, replace = TRUE),
total = rnorm(1000, mean = 0, sd = 1))
# find create a column with the frequency values for each clade variable value
df <- df %>%
group_by(clade) %>%
mutate(freq = n()/nrow(.))
# plot the frequencies
barplot(prop.table(table(df$clade)))
這段代碼做的是首先要複製你指的是數據,第二我創建了一個變量在您的數據框中稱爲freq
,它顯示了數據中Clade
變量值的相對頻率。最後,我繪製了Clade
變量值的相對頻率。
歡迎來到StackOverflow!請閱讀關於[如何提出一個好問題](http://stackoverflow.com/help/how-to-ask)以及如何給出[可重現的示例]的信息(http://stackoverflow.com/questions/ 5963269 /如何對化妝一個偉大-R-重複性,例如/ 5963610)。這會讓其他人更容易幫助你。 – Jaap
對於靈感,請參閱:[*是否有一個聚合FUN選項來計算事件?*](http://stackoverflow.com/questions/9809166/is-there-an-aggregate-fun-option-to-count-occurrences ) – Jaap