Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
346 views
in Technique[技术] by (71.8m points)

r - Merge Panel data to get balanced panel data

I have several data frames in panel data form. Now I want to merge these panel data frames into one panel data. These data frames have common and different between them. I illustrate as follows:

df1:

Month   variable    Beta1   Beta2   Beta3   Beta4   Beta5   Beta6
Jan-05     A        1       2       3       4       5       6
Feb-05     A        2       3       4       5       6       7
Mar-05     A        3       4       5       6       7       8
Apr-05     A        4       5       6       7       8       9
May-05     A        5       6       7       8       9       10
Jun-05     A        6       7       8       9      10       11
Jul-05     A        7       8       9       10     11       12
Aug-05     A        8       9       10      11     12       13
Sep-05     A        9       10      11      12     13       14
Oct-05     A       10       11      12      13     14       15
Nov-05     A       11       12      13      14     15       16
Dec-05     A       12       13      14      15     16       17
Jan-05     B       12       12      12      12     12       12
Feb-05     B       12       12      12      12     12       12
Mar-05     B       12       12      12      12     12       12
Apr-05     B       12       12      12      12     12       12
May-05     B       12       12      12      12     12       12
Jun-05     B       12       12      12      12     12       12
Jul-05     B       12       12      12      12     12       12
Aug-05     B       12       12      12      12     12       12
Sep-05     B       12       12      12      12     12       12
Oct-05     B       12       12      12      12     12       12
Nov-05     B       12       12      12      12     12       12
Dec-05     B       12       12      12      12     12       12

df2:

Month   variable    Beta1   Beta2   Beta3   Beta4   Beta5   Beta6
Jan-06     A        1       2       3       4       5       6
Feb-06     A        2       3       4       5       6       7
Mar-06     A        3       4       5       6       7       8
Apr-06     A        4       5       6       7       8       9
May-06     A        5       6       7       8       9       10
Jun-06     A        6       7       8       9      10       11
Jul-06     A        7       8       9       10     11       12
Aug-06     A        8       9       10      11     12       13
Sep-06     A        9       10      11      12     13       14
Oct-06     A       10       11      12      13     14       15
Nov-06     A       11       12      13      14     15       16
Dec-06     A       12       13      14      15     16       17
Jan-06     C       12       12      12      12     12       12
Feb-06     C       12       12      12      12     12       12
Mar-06     C       12       12      12      12     12       12
Apr-06     C       12       12      12      12     12       12
May-06     C       12       12      12      12     12       12
Jun-06     C       12       12      12      12     12       12
Jul-06     C       12       12      12      12     12       12
Aug-06     C       12       12      12      12     12       12
Sep-06     C       12       12      12      12     12       12
Oct-05     C       12       12      12      12     12       12
Nov-05     C       12       12      12      12     12       12
Dec-05     C       12       12      12      12     12       12

The desired output is as follows, I want to merge the panel data frames such that each variable arranged chronically and if the data is unable for a year then it is it has NAs under the Beta1, Beta2 and so on.

 Month  variable    Beta1   Beta2   Beta3   Beta4   Beta5   Beta6
Jan-05    A            1    2       3       4       5        6
Feb-05    A            2    3       4       5       6        7
Mar-05    A            3    4       5       6       7        8
Apr-05    A            4    5       6       7       8        9
May-05    A            5    6       7       8       9       10
Jun-05    A            6    7       8       9       10      11
Jul-05    A            7    8       9       10      11      12
Aug-05    A            8    9       10      11      12      13
Sep-05    A            9    10      11      12      13      14
Oct-05    A            10   11      12      13      14      15
Nov-05    A            11   12      13      14      15      16
Dec-05    A            12   13      14      15      16      17
Jan-06    A            1    2        3       4       5      6
Feb-06    A            2    3        4       5       6      7
Mar-06    A            3    4        5       6       7      8
Apr-06    A            4    5        6       7       8      9
May-06    A            5    6        7       8       9     10
Jun-06    A            6    7        8       9       10    11
Jul-06    A            7    8        9      10       11    12
Aug-06    A            8    9        10     11       12    13
Sep-06    A            9    10       11     12       13    14
Oct-06    A           10    11      12      13       14    15
Nov-06    A           11    12      13      14       15    16
Dec-06    A           12    13      14      15       16    17
Jan-05    B           12    12      12      12       12    12
Feb-05    B           12    12      12      12       12    12
Mar-05    B           12    12      12      12       12    12
Apr-05    B           12    12      12      12       12    12
May-05    B           12    12      12      12       12    12
Jun-05    B           12    12      12      12       12    12
Jul-05    B           12    12      12      12       12    12
Aug-05    B           12    12      12      12       12    12
Sep-05    B           12    12      12      12       12    12
Oct-05    B           12    12      12      12       12    12
Nov-05    B           12    12      12      12       12    12
Dec-05    B           12    12      12      12       12    12
Jan-06    B           NA    NA      NA      NA       NA    NA
Feb-06    B           NA    NA      NA      NA       NA    NA
Mar-06    B           NA    NA      NA      NA       NA    NA
Apr-06    B           NA    NA      NA      NA       NA    NA
May-06    B           NA    NA      NA      NA       NA    NA
Jun-06    B           NA    NA      NA      NA       NA    NA
Jul-06    B           NA    NA      NA      NA       NA    NA
Aug-06    B           NA    NA      NA      NA       NA    NA
Sep-06    B           NA    NA      NA      NA       NA    NA
Oct-06    B           NA    NA      NA      NA       NA    NA
Nov-06    B           NA    NA      NA      NA       NA    NA
Dec-06    B           NA    NA      NA      NA       NA    NA
Jan-05    C           NA    NA      NA      NA       NA    NA
Feb-05    C           NA    NA      NA      NA       NA    NA
Mar-05    C           NA    NA      NA      NA       NA    NA
Apr-05    C           NA    NA      NA      NA       NA    NA
May-05    C           NA    NA      NA      NA       NA    NA
Jun-05    C           NA    NA      NA      NA       NA    NA
Jul-05    C           NA    NA      NA      NA       NA    NA
Aug-05    C           NA    NA      NA      NA       NA    NA
Sep-05    C           NA    NA      NA      NA       NA    NA
Oct-05    C           NA    NA      NA      NA       NA    NA
Nov-05    C           NA    NA      NA      NA       NA    NA
Dec-05    C           NA    NA      NA      NA       NA    NA
Jan-06    C           12    12      12      12       12    12
Feb-06    C           12    12      12      12       12    12
Mar-06    C           12    12      12      12       12    12
Apr-06    C           12    12      12      12       12    12
May-06    C           12    12      12      12       12    12
Jun-06    C           12    12      12      12       12    12
Jul-06    C           12    12      12      12       12    12
Aug-06    C           12    12      12      12       12    12
Sep-06    C           12    12      12      12       12    12
Oct-06    C           12    12      12      12       12    12
Nov-06    C           12    12      12      12       12    12
Dec-06    C           12    12      12      12       12    12

As I mentioned earlier that I several data frames and merging them would probably result in hundred thousand rows, so I could I tackle the memory and space issues. I would really appreciate your help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There's a function for that. Combine the data frames with rbind. Then use complete. It will look through the groups in variable and fill any with missing values:

library(tidyr)
df3 <- do.call(rbind.data.frame, list(df1, df2))
df3$Month <- as.character(df3$Month)
df4 <- complete(df3, Month, variable)
df4$Month <- as.yearmon(df4$Month, "%b %Y")
df5 <- df4[order(df4$variable,df4$Month),]
df5
# Source: local data frame [72 x 8]
# 
#       Month variable Beta1 Beta2 Beta3 Beta4 Beta5 Beta6
#      (yrmn)   (fctr) (int) (int) (int) (int) (int) (int)
# 1  Jan 2005        A     1     2     3     4     5     6
# 2  Feb 2005        A     2     3     4     5     6     7
# 3  Mar 2005        A     3     4     5     6     7     8
# 4  Apr 2005        A     4     5     6     7     8     9
# 5  May 2005        A     5     6     7     8     9    10
# 6  Jun 2005        A     6     7     8     9    10    11
# 7  Jul 2005        A     7     8     9    10    11    12
# 8  Aug 2005        A     8     9    10    11    12    13
# 9  Sep 2005        A     9    10    11    12    13    14
# 10 Oct 2005        A    10    11    12    13    14    15
# ..      ...      ...   ...   ...   ...   ...   ...   ...

An alternative implementation with dplyr & tidyr:

library(dplyr)
library(tidyr)

df3 <- bind_rows(df1, df2) %>% 
  complete(Month, variable)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...