【问题标题】:How can I create a dataframe combining all values of different variables (R)?如何创建一个组合不同变量 (R) 的所有值的数据框?
【发布时间】:2021-07-10 15:59:12
【问题描述】:

我想创建一个数据框,其中包含 id、年份和月份列以及以下值的所有组合。

id <- c(1002,463,546,9787,849,34)
years <- 2000:2009
months <- 1:12

在这个简单的示例中,我要获取的数据帧的总行数应该有 6*10*12 行。每个6 id 都应显示在10*12 行中,每个都指给定的年月。

【问题讨论】:

    标签: r loops


    【解决方案1】:

    试试expand.grid

    expand.grid(id,years,months)
    

    【讨论】:

    • 还有expand_grid(id, years, months)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-23
    • 2016-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多