SAJ
SAJ

Reputation: 320

How can I make my code more efficient in R - It's to repetitive

I have a question regarding the efficiency of my code. I have 9 data frames in my environment and for each of them I need to perform the same steps. The steps and the code is (only shown for two of the data frames):

CDL <- aggregate(A$Frequency, by=list(Category=A$Words), FUN=sum)
wordcloud(words = CDL$Category, freq = CDL$x, min.freq = 2,
      max.words=250, random.order=FALSE, rot.per=0.35, 
      colors=brewer.pal(6, "Dark2"))

Ltd <- aggregate(B$Frequency, by=list(Category=B$Words), FUN=sum)
wordcloud(words = Ltd$Category, freq = Ltd$x, min.freq = 2,
      max.words=250, random.order=FALSE, rot.per=0.35, 
      colors=brewer.pal(6, "Dark2"))

I first aggregate all the same words, sum their frequencies and then create a world cloud based on the aggregated results.

The object names in the environment start from 'A' and go all the way to 'I'. The variable 'Frequency' is just a number, the variable 'Words' contains a list of the words.

For the wordcloud: The variable 'Category' contains the unique words taken from the 'Words' variable and 'x' is the aggregated sum of Frequencies taken from 'Frequency'

Is there any way I could perform the same but without repeating my code? Thanks

Upvotes: 1

Views: 73

Answers (1)

tobiaspk1
tobiaspk1

Reputation: 388

If I am not mistaken, defining a function and loop it should work!

word_cloud <- function(df) {
  temp <- aggregate(df[,"Frequency"], by=list(Category=df[,"Words"]), FUN=sum)
  result <- wordcloud(words = temp[,"Category"], 
    freq = temp[,"x"],
    min.freq = 2,
    max.words=250,
    random.order=FALSE,
    rot.per=0.35,
    colors=brewer.pal(6, "Dark2"))
  return(result)
}

input_list <- list(A,B,C,D,E,F,G,H,I)

for (df_inp in input_list) {
  word_cloud(df_inp)
}

I do not know the output mode of wordcloud(), so you may want to save the results to a list or plot it! I hope that helps!

Upvotes: 2

Related Questions