[英]How to combine multiple data frames in a list in for loop in R?
我有一個類似於如下所示的代碼:
# initialize an empty list of dataframes here....
for (i in range 1:10) {
# create a new data frame here....
# append this newly created dataframe to the list here....
如何在循環開始時創建一個空的數據幀列表,然后在 for 循環的每次迭代中繼續添加新創建的數據幀?
如果唯一的目的是合並數據框,使用reshape
包中的merge_all
可能更容易:
reshape::merge_all(your_list_with_dfs, ...)
或者,您可以嘗試:
do.call("rbind", your_list_with_dfs)
為了追加行。
我這樣做的方式如下(如@akrun 所建議)
list_of_frames <- replicate(10, data.frame())
for (i in range 1:10) {
# create a new data frame called new_dataframe
list_of_frames[[i]] <- new_dataframe
myList <- vector(mode = "list", length = 10) # Create an empty list.
# Length depends on how many dataframe you want to append.
for (i in seq(1,10)) {
myList[[i]] <- new_dataframe #append your new dataframe
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.