[英]R for loop: creating data frames using split?
我有要按日期分隔的數據,我已設法通過以下方式手動執行此操作:
tsssplit <- split(tss, tss$created_at)
然后為我使用的每個列表創建數據框。
t1 <- tsssplit[[1]]
t2 <- tsssplit[[2]]
但我不知道我需要多少拆分,因為有時 og 數據框可能有 6 個日期要拆分,有時可能有 5 個,等等。所以我想創建一個 for 循環。
在 for 循環中,我想合並此代碼,它連接到 function:
bscore3 <- score.sentiment(t3$cleaned_text,pos.words,neg.words,.progress='text')
score3 <- as.integer(bscore3$score[[1]])
然后我希望能夠創建一個新的數據框,其中包含每個列表的分數。
所以本質上我希望for循環:
只要我能在最后想出分數的可視化,就不必完全像這樣。
謝謝!
不建議在全局環境中創建單獨的數據框,它們很難跟蹤。 而是將它們放在一個列表中。 通過使用split
和創建數據框列表,您已經很好地開始了。 然后,您可以遍歷列表中的每個 dataframe 並在每個上應用 function。
按此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.