答案 0 :(得分:1)
f=read.csv("~/Downloads/moviegenres.csv")
table_f=as.matrix(table(f))
for (i in 1:4){ #Since there are four unique movie genres,for each of them
#search maximum count(find popular director) then paste name and the max number
print(paste(names(table_f[i,][table_f[i,]==max(table_f[i,])]),max((table_f[i,]))))
}
希望这会有所帮助。
答案 1 :(得分:0)
您实际上不需要循环来计数。这是按频率表进行计数的方法。
library(dplyr)
df %>% table
# rearrange to show mode
df %>% table %>% as_tibble() %>% arrange(desc(n))
或者在基数R中,您可以使用
tb=as.data.frame(table(df$Genres, df$Directors))
head(tb[order(tb$Freq, decreasing = T),])