【问题标题】:Bind 2 dataframes with dataframes elements将 2 个数据框与数据框元素绑定
【发布时间】:2021-10-13 23:27:05
【问题描述】:

我需要绑定 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)
)

Arguments 元素在这里是数据框。

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

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

[&lt;-.data.frame(*tmp*, ri, , value = list(MaxCommitedGB = 82, : 提供 3 个变量来替换 2 个变量 dim(rvec)

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

allocate_column(df[[var]], nrows, dfs, var) 中的错误: rbind.fill 不支持数据框列“参数”

bind_rows(issues.df_1, issues.df_2)

dim(rvec)

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

【问题讨论】:

    标签: r dataframe dplyr plyr rbind


    【解决方案1】:

    解决方案是 - 使用 tibble 代替 data.frame。那个 case 命令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
    

    【讨论】:

      猜你喜欢
      • 2013-09-20
      • 2019-03-15
      • 1970-01-01
      • 2021-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-01
      • 1970-01-01
      相关资源
      最近更新 更多