简体   繁体   English

R for 循环:使用拆分创建数据帧?

[英]R for loop: creating data frames using split?

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.但我不知道我需要多少拆分,因为有时 og 数据框可能有 6 个日期要拆分,有时可能有 5 个,等等。所以我想创建一个 for 循环。

Within the for loop, I want to incorporate this code, which connects to a function:在 for 循环中,我想合并此代码,它连接到 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:所以本质上我希望for循环:

  1. split the data into lists using split使用 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!谢谢!

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.通过使用split和创建数据框列表,您已经很好地开始了。 You can then iterate over each dataframe in the list and apply the function on each one of them.然后,您可以遍历列表中的每个 dataframe 并在每个上应用 function。

Using by this would look like as:按此by将如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM