1

I am new to for loops and I am struggling to accomplish something that is probably quite basic. This code is running several similarity statistics in the loop. However, when I try to name each dataframe column using the i from the for loop(i.e. results$i), it just names each iteration of the loop "i", creating duplicates of the variable "results$i".

similarities <- c("correlation", "cosine", "jaccard", "ejaccard", "dice", "edice")

for (i in similarities){
  print(i)
  tstat <- textstat_simil(corp_dfm, method = i, margin = "documents")
    results <- as.data.frame(as.matrix(tstat))
    results$i <- rowSums(results)
    results$i <- results$i - 1
    results$i <- results$i/49
    results$name <- results$row.names
    data2 <- results
    data2 <- tibble::rownames_to_column(data2, "name")
    sub_similarity <- subset(data2, select = c(name, i))
    similarity_scores <- merge(similarity_scores, sub_similarity, by.x = "name", by.y = "name" )
}

But I want the assigned dataframe column name to change with each iteration (e.g., results$correlation, results$cosine, results$jaccard, etc. Is there a way to do this within the loop I've already created?

lwe
  • 323
  • 1
  • 8

0 Answers0