将 2 个数据框与数据框元素绑定

Bind 2 dataframes with dataframes elements

我需要绑定 2 个包含数据框作为元素的数据框。问题 - 嵌入的数据框列名可能不同,列数可能不同。这就是我需要的结果:

这是我的数据:


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)
)

参数元素在这里是数据框。

我正在尝试不同的方法来绑定数据帧,但我遇到了错误。

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

警告 [<-.data.frame(*tmp*, ri, , value = list(MaxCommitedGB = 82, : 提供 3 个变量来替换 2 个变量 dim(rvec) <- dim(x) 中的错误: dims [product 6] 与对象 [2]

的长度不匹配

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

allocate_column(df[[var]], nrows, dfs, var) 错误: rbind.fill

不支持数据框列 'Arguments'

bind_rows(issues.df_1, issues.df_2)

dim(rvec) <- dim(x) 错误: dims [product 8] 与对象 [4]

的长度不匹配

如何从我拥有的 2 个数据帧创建我需要的数据帧?

解决方案是 - 使用 tibble 而不是 data.frame。该案例命令 dplyr::bind_rows(issues.df_1, issues.df_2) 给出了正确的结果。

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)

结果:

# 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