R - combine data frames of different lengths after loop

thiagoveloso

I am looping through a series of 15 files. My loop looks like this:

for (i in 1:length(mod.f)) {
    do some stuff
    dataframe `df` is produced
}

Each iteration of the loop will yield a data frame with two columns - date and some value, just like the example below:

df <- structure(list(date = structure(c(-43829, -43798, -43770, -43739, 
-43709, -43678, -43648, -43617, -43586, -43556, -43525, -43495, 
-43464, -43433, -43405, -43374, -43344, -43313, -43283, -43252
), class = "Date"), inmcm4 = c(71.4782417258324, 68.5037706662898, 
64.0571482842429, 62.8708849771957, 66.3121740437669, 62.7535770507166, 
62.2819567665719, 62.3014754255822, 58.6247123853888, 58.4425949480101, 
61.3534245382973, 68.2531958750396, 70.4892992599108, 70.1840748468477, 
64.6298343911645, 66.5280510648649, 65.2767506692563, 62.8944646174169, 
60.4309882672837, 58.7368776782633)), .Names = c("date", "inmcm4"
), row.names = c(NA, 20L), class = "data.frame")

The problem here is that not all the data frames have the same length. The lengths of each data frame are the following:

1872 
1740 
1872 
1932 
1872 
1752 
1752 
1872 
1872 
672 
1872 
1872 
1956 
1956 
1872

As a result, not all dates (e.g. df$date) will be the same.

What I wish to do is to combine all data frames in one single data frame by the end of the loop. This final data frame should have a date column and the remaining columns would be the values of each iteration, assigning NA's to the dates that don't overlap.

Any ideas on how to do this?

Thanks!

akrun

You can try

 Reduce(function(...) merge(..., by='date', all=TRUE), lst)

where lst is the list of data.frames

data

set.seed(24)
df2 <- df[sample(1:nrow(df),8, replace=FALSE),]
row.names(df2) <- NULL
lst <- list(df, df2)

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to combine two variables from different data frames in R?

分類Dev

Create a table from different data frames in R

分類Dev

r multiple joins from list of data frames of differing lengths and differing keys

分類Dev

Null multiple data frames in R

分類Dev

Appending Data Frames to a list in R

分類Dev

rbind a list of data frames with different columns

分類Dev

Merging many CSVs into different data frames

分類Dev

Saving multiple data frames from loop

分類Dev

Loop through data frames to produce new column

分類Dev

Summing data-frame columns from different data-frames

分類Dev

Cross comparisons of R data-frames

分類Dev

R and Simmer: Performance boost on large data frames

分類Dev

How to merge two data frames by ranges in R?

分類Dev

Pair Correlations in R with mixed types in Data Frames

分類Dev

Divide a Column based on two Data Frames in R

分類Dev

python - pandas, how to map the same value with different data frames?

分類Dev

Extract different set of rows from data frames in list

分類Dev

Create multiple data frames from one based off values with a for loop

分類Dev

merge 2 data frames in a loop for each column in one of them

分類Dev

Read multiple xml files in R and combine the data

分類Dev

Combine rows of data frame in R using colMeans?

分類Dev

Taking column mean over a list of data frames in R

分類Dev

R - How to subset certain rows out of multiple data frames

分類Dev

Average data frames with common characters in their name in a list in R

分類Dev

A set of functions over multiple data frames and merge the outputs in R

分類Dev

Save several data.frames in the environment of R using for

分類Dev

Check if dates and times overlap for two separate data frames in R

分類Dev

Finding different characters in strings of different lengths

分類Dev

combine 2 similars list of data frame into one list using a loop

Related 関連記事

  1. 1

    How to combine two variables from different data frames in R?

  2. 2

    Create a table from different data frames in R

  3. 3

    r multiple joins from list of data frames of differing lengths and differing keys

  4. 4

    Null multiple data frames in R

  5. 5

    Appending Data Frames to a list in R

  6. 6

    rbind a list of data frames with different columns

  7. 7

    Merging many CSVs into different data frames

  8. 8

    Saving multiple data frames from loop

  9. 9

    Loop through data frames to produce new column

  10. 10

    Summing data-frame columns from different data-frames

  11. 11

    Cross comparisons of R data-frames

  12. 12

    R and Simmer: Performance boost on large data frames

  13. 13

    How to merge two data frames by ranges in R?

  14. 14

    Pair Correlations in R with mixed types in Data Frames

  15. 15

    Divide a Column based on two Data Frames in R

  16. 16

    python - pandas, how to map the same value with different data frames?

  17. 17

    Extract different set of rows from data frames in list

  18. 18

    Create multiple data frames from one based off values with a for loop

  19. 19

    merge 2 data frames in a loop for each column in one of them

  20. 20

    Read multiple xml files in R and combine the data

  21. 21

    Combine rows of data frame in R using colMeans?

  22. 22

    Taking column mean over a list of data frames in R

  23. 23

    R - How to subset certain rows out of multiple data frames

  24. 24

    Average data frames with common characters in their name in a list in R

  25. 25

    A set of functions over multiple data frames and merge the outputs in R

  26. 26

    Save several data.frames in the environment of R using for

  27. 27

    Check if dates and times overlap for two separate data frames in R

  28. 28

    Finding different characters in strings of different lengths

  29. 29

    combine 2 similars list of data frame into one list using a loop

ホットタグ

アーカイブ