循环遍历两个数据帧

时间:2021-07-18 16:48:20

标签: r function loops

我有以下代码行,我想使用下面的命令对男性和女性词进行文本分析

words_male <- subset(Dec_10_Spreadsheet, Gender=="Male")
words_female<- subset(Dec_10_Spreadsheet, Gender="Female")

words_female<- words_female %>% 
  select(Text) %>%
  unnest_tokens(word,Text) %>%
  anti_join(stop_words) %>%
  filter(word != "meeting", word != "speaking", word != "government", word != "house", word!= "question", words!="issue", word!="policy",word!="speaker")



words_female %>% 
  count(word,sort=T) %>% 
  slice(1:20) %>% 
  ggplot(aes(x = reorder(word, n, function(n) -n), y = n)) + 
  geom_bar(stat = "identity") + 
  theme(axis.text.x = element_text(angle = 60, hjust = 1)) + 
  xlab("")

words_male<- words_male %>% 
  select(Text) %>%
  unnest_tokens(word,Text) %>%
  anti_join(stop_words) 


words_male %>% 
  count(word,sort=T) %>% 
  slice(1:20) %>% 
  ggplot(aes(x = reorder(word, n, function(n) -n), y = n)) + 
  geom_bar(stat = "identity") + 
  theme(axis.text.x = element_text(angle = 60, hjust = 1)) + 
  xlab("")


我想优化此代码,使其循环遍历男性和女性数据集,而无需重复两次行。我可能还需要将这些数据进一步细分为许多不同的子集,因此拥有这样的代码将非常有帮助。有任何想法吗?我会送你一杯星巴克咖啡 :)

0 个答案:

没有答案