Home > Software design >  Save results in dataframe
Save results in dataframe

Time:05-12

score.sentiment <- function(sentences, valence1, .progress='none')
{
  require(plyr)
  require(stringr)
  sentence <- tolower(sentence) #to lower case
  val.matches1 <- str_count(sentence, valence1) #find words in dictionary
  return(val.matches1)
}
final_scores <- matrix('', 0, 20)
for(i in mytxtfiles){
  print(i)
  tryCatch(
    {
      sentence <- readLines(i, warn = FALSE)
      sentence <- paste(sentence, collapse = ' ')
      #Uses the created function to assign disctionary scores to texts
      positive <- score.sentiment(sentence, valence1, .progress='text') 
      filename<-i
      result<-cbind(filename,positive)
      final_scores <- rbind(final_scores, result)
    }, 
    error=function(e){}
  )
}

If I do print(positive) in my for loop, I have :

[1] 0 0 0 0 0 0 0 0 0 0 8 0 0 1 0 0 0 0 0

I would like to save the output of my function into a dataframe, then row binding it each time it loops through a text, and ending up with a database with one column for the filename and 19 column for the output of str_count, but somehow my dataframe is empty at the end of the process. Any helps appreciated.

EDIT: Using "val.matches1 <- (t(val.matches1))" in my function, I transpose the column with 19 rows into one row and 19 columns. How can I add them to my dataframe ?

CodePudding user response:

To update the dataframe with values inside a loop and without the overhead associated with repeatedly calling bind, define the data frame outside the loop with the proper size and then assign values row by row.

Since I don't know what variable "mytxtfiles" represents, I was not able to test this but it should point you in the correct direction. See comments for an explanation.

mylength <- length(mytxtfiles)
#initialize the dataframe to the desired length
final_scores <- data.frame(filename = character(length=mylength), positive = vector(length=mylength))
#initialize the index
index <-0

for(i in mytxtfiles){
   print(i)
   tryCatch(
      {
         sentence <- readLines(i, warn = FALSE)
         sentence <- paste(sentence, collapse = ' ')
         #Uses the created function to assign disctionary scores to texts
         positive <- score.sentiment(sentence, valence1, .progress='text') 
         #increament the index and store the values
         index <- index  1
         final_scores[index, "filename"]<- i
         final_scores[index, "positive"]<- positive
      }, 
      error=function(e){}
   )
}

CodePudding user response:

map_dcf(valence1, function(valence1){
tibble(!!sym(valence1) := sapply(sentence, str_count, pattern=valence1))
}) %>% mutate(file=i)

Ended up doing this.

  • Related