Search code examples
rdataframedplyrplyrrbind

Bind 2 dataframes with dataframes elements


I need to bind 2 dataframes that contain dataframe as element. The problem - inlayed dataframes column names could be different, and number of columns could be different. That's the result I need:

enter image description here

Here is my data:


issue_desc_1 <- data.frame(
                   MinAvailableMb = 50,
                   Threshold = 100
                   )

issues.df_1 <- data.frame(
    IssueId = 1,
    IssueSubId = 1,
    Categories = "Cat1",
    Solution = "Sol1",
    Level = 'Critical',
    AffectedObjects = "comp1.domain.local",
    Arguments = I(issue_desc_1)
)


issue_desc_2 <- data.frame(
                     MaxCommitedGB = 82,
                     RamSize = 64,
                     Threshold = 64
)

    
issues.df_2 <- data.frame(
    IssueId = 1,
    IssueSubId = 2,
    Categories = "Cat1",
    Solution = "Sol1",
    Level = 'Critical',
    AffectedObjects = "comp2.domain.local",
    Arguments = I(issue_desc_2)
)

Arguments element is dataframe here.

I am trying different ways to bind dataframes but I'm getting errors.

rbind(issues.df_1, issues.df_2) and rbind(issues.df_1, issues.df_2, fill = T)

Warning in [<-.data.frame(*tmp*, ri, , value = list(MaxCommitedGB = 82, : provided 3 variables to replace 2 variables Error in dim(rvec) <- dim(x) : dims [product 6] do not match the length of object [2]

plyr::rbind.fill(issues.df_1, issues.df_2)

Error in allocate_column(df[[var]], nrows, dfs, var) : Data frame column 'Arguments' not supported by rbind.fill

bind_rows(issues.df_1, issues.df_2)

Error in dim(rvec) <- dim(x) : dims [product 8] do not match the length of object [4]

How to create dataframe I need from 2 dataframes I have?


Solution

  • The solution is - to use tibble instead of data.frame. That case command dplyr::bind_rows(issues.df_1, issues.df_2) gives correct result.

    library(jsonlite)
    library(dplyr)
    
    
    issue_desc_1 <- tibble(
                       MinAvailableMb = 50,
                       Threshold = 100
                       )
    
    issues.df_1 <- tibble(
        IssueId = 1,
        IssueSubId = 1,
        Categories = "Cat1",
        Solution = "Sol1",
        Level = 'Critical',
        AffectedObjects = "comp1.domain.local",
        Arguments = (issue_desc_1)
    )
    
    
    issue_desc_2 <- tibble(
                         MaxCommitedGB = 82,
                         RamSize = 64,
                         Threshold = 64
    )
    
    issues.df_2 <- tibble(
        IssueId = 1,
        IssueSubId = 2,
        Categories = "Cat1",
        Solution = "Sol1",
        Level = 'Critical',
        AffectedObjects = "comp2.domain.local",
        Arguments = (issue_desc_2)
    )
    
    
    dplyr::bind_rows(issues.df_1, issues.df_2)
    

    The result:

    # A tibble: 2 x 7
      IssueId IssueSubId Categories Solution Level    AffectedObjects    Arguments$MinAvailableMb $Threshold $MaxCommitedGB $RamSize
        <dbl>      <dbl> <chr>      <chr>    <chr>    <chr>                                 <dbl>      <dbl>          <dbl>    <dbl>
    1       1          1 Cat1       Sol1     Critical comp1.domain.local                       50        100             NA       NA
    2       1          2 Cat1       Sol1     Critical comp2.domain.local                       NA         64             82       64