0

The below code executes successfully but it stops if it finds stats dataframe has got 0 rows and its not executing and rbind the next set of rows that have more than 0 rows, so as to encounter that I have put a next statement still it stops. Please help me out!

textrank<-NULL
for(i in lista){
  if(i %in% dflist_T$VariaF){
    Eachdesc <- subset(dflist_T, VariaF %in% i)
    ud_model <- udpipe_load_model(model$file_model)
    x <- udpipe_annotate(ud_model,x = Eachdesc$Description)

    x <- as.data.frame(x)
    stats <- keywords_rake(x = x, 
                           term = "token", group = c("doc_id", "paragraph_id", "sentence_id"),
                           relevant = x$upos %in% c("NOUN", "ADJ"),
                           ngram_max = 4)
    stats$Origin <- "dflist"
    stats$lista <- i
    if (nrow(stats) == 0) {
      next}
    textrank<-rbind(textrank,stats)
  }
}
prog
  • 1,073
  • 5
  • 17

1 Answers1

1

You can just rbind dependently.

edit: you should also check whether textrank already exists. So I included an is.null() check

textrank <- NULL
for(i in lista){
  if(i %in% dflist_T$VariaF){
    Eachdesc <- subset(dflist_T, VariaF %in% i)
    ud_model <- udpipe_load_model(model$file_model)
    x <- udpipe_annotate(ud_model,x = Eachdesc$Description)

    x <- as.data.frame(x)
    stats <- keywords_rake(x = x, 
                           term = "token", group = c("doc_id", "paragraph_id", "sentence_id"),
                           relevant = x$upos %in% c("NOUN", "ADJ"),
                           ngram_max = 4)
    stats$Origin <- "dflist"
    stats$lista <- i

    # check for non empty stats and existent textrank
    if(nrow(stats) != 0 & !is.null(textrank)) {
      textrank <- rbind(textrank, stats)
    }
  }
}
mnist
  • 6,571
  • 1
  • 18
  • 41
  • still i get error saying ```Error in `$<-.data.frame`(`*tmp*`, "Origin", value = "dflist") : replacement has 1 row, data has 0``` – prog Nov 19 '19 at 15:24
  • Then please provide a [reproducible minimal example](https://stackoverflow.com/q/5963269/8107362). Especially, provide some sample data, e.g. with `dput()`. – mnist Nov 19 '19 at 15:30