0

I have data that I want to separate by date, I have managed to do this manually through:

tsssplit <- split(tss, tss$created_at)

and then creating dataframes for each list which I then use.

t1 <- tsssplit[[1]]

t2 <- tsssplit[[2]]

But I don't know how many splits I will need, as sometimes the og data frame may may have 6 dates to split up by, and sometimes it may have 5, etc. So I want to create a for loop.

Within the for loop, I want to incorporate this code, which connects to a function:

bscore3 <- score.sentiment(t3$cleaned_text,pos.words,neg.words,.progress='text')
score3 <- as.integer(bscore3$score[[1]])

Then I want to be able to create a new data frame that has the scores for each list.

So essentially I want the for loop to:

  1. split the data into lists using split
  2. split each list into a separate data frames for each different day
  3. Come out with a score for each data frame
  4. Put that into a new data frame

It doesn't have to be exactly like this as long as I can come up with a visualisation of the scores at the end.

Thanks!

jay
  • 31
  • 3
  • 1
    I suggest you keep the data as a list of frames, and use `lapply(tsssplit, function(x) ...)` instead of a `for` loop. See https://stackoverflow.com/a/24376207/3358227 – r2evans Jan 11 '21 at 02:05

1 Answers1

0

It is not recommended to create separate dataframes in the global environment, they are difficult to keep track of. Put them in a list instead. You have started off well by using split and creating list of dataframes. You can then iterate over each dataframe in the list and apply the function on each one of them.

Using by this would look like as :

by(tss, tss$created_at, function(x) {
  bscore3 <- score.sentiment(x$cleaned_text,pos.words,neg.words,.progress='text')
  score3 <- as.integer(bscore3$score[[1]])
  return(score3)
}) -> result

result
Ronak Shah
  • 286,338
  • 16
  • 97
  • 143
  • Hey Ronak, thanks so much for your answer this worked perfectly! Although I'm wondering how would I combine this with another loop? I have the form now where tss is now a list made from another by function, and when i try to do this it comes up as "Error in as.data.frame.default(data) : cannot coerce class ‘"by"’ to a data.frame" – jay Jan 14 '21 at 22:24
  • You might need `result – Ronak Shah Jan 14 '21 at 23:19