【问题标题】:create dataframe with dummies用假人创建数据框
【发布时间】:2021-06-11 10:40:06
【问题描述】:

我是 R 新手,手头有问题。我基本上想创建一个数据框,其中包含每年有人拥有房子的虚拟变量。 0 表示他没有卖出的每一年,1 表示他卖出的那一年。在此旁边,我需要年份 iteslf 和买房子的年份,因为我有一个单独的数据集,其中包含每年的房价指数。 我想这最好通过某种循环来完成,因为我有 40.000 笔交易

我现在拥有的示例数据

Buy Sold
1620 1624
1622 1628

然后我需要它变成什么

dummy year bought current year
0 1620 1621
0 1620 1622
0 1620 1623
1 1620 1624
0 1622 1623
0 1622 1624
0 1622 1625
0 1622 1626
0 1622 1627
1 1622 1628

那么最终我还需要另一列,其中包含当年房价指数之间的价格差异 - 房屋购买年份的价格指数。我确实有一个单独的数据集,其中包含每年的价格指数。我不知道该怎么做,但我想一旦我弄清楚了这些数据,这将相对容易。提前致谢!

【问题讨论】:

    标签: r dataframe dummy-variable


    【解决方案1】:

    tidyverse 选项 -

    library(tidyverse)
    
    df %>%
      mutate(current_year = map2(Buy + 1, Sold, seq)) %>%
      unnest(current_year) %>%
      mutate(dummy = as.integer(Sold == current_year), .before = 1) %>%
      select(-Sold, Year_bought = Buy)
    
    #   dummy Year_bought current_year
    #   <int>       <int>        <int>
    # 1     0        1620         1621
    # 2     0        1620         1622
    # 3     0        1620         1623
    # 4     1        1620         1624
    # 5     0        1622         1623
    # 6     0        1622         1624
    # 7     0        1622         1625
    # 8     0        1622         1626
    # 9     0        1622         1627
    #10     1        1622         1628
    

    map2BuySold 之间创建一个序列,当current_year 与销售年份相同时,创建一个dummy 列。

    数据

    df <- structure(list(Buy = c(1620L, 1622L), Sold = c(1624L, 1628L)),
          class = "data.frame", row.names = c(NA, -2L))
    

    【讨论】:

      【解决方案2】:

      data.table 解决方案:

      library(data.table)
      
      data <- data.table(Buy = c(1620, 1622), Sell = c(1624, 1628))
      data <- data[, .(`year bought` = Buy, `current year` = seq(Buy, Sell, by = 1)), , 
                   .(grp = 1:nrow(data))][, grp := NULL]
      data[, dummy := ifelse(`current year` == max(`current year`), 1, 0), by=.(`year bought`)]
      

      我确实想补充一点,如果有超过 1 套房屋具有相同的购买年份,或者还有尚未售出的房屋,这将成为问题。如果您的数据中存在其中任何一个,我可以稍微修改代码。

      【讨论】:

        【解决方案3】:

        另一种方法

        df <- structure(list(Buy = c(1620L, 1622L), Sold = c(1624L, 1628L)),
                        class = "data.frame", row.names = c(NA, -2L))
        
        library(tidyverse)
        df %>% group_by(grp = data.table::rleid(Buy)) %>%
          uncount(Sold - Buy) %>%
          mutate(current_year = first(Buy) + row_number()) %>%
          ungroup() %>%
          mutate(Sold = +(Sold == current_year)) %>%
          select(dummy = Sold, year_bought = Buy, current_year)
        #> # A tibble: 10 x 3
        #>    dummy year_bought current_year
        #>    <int>       <int>        <int>
        #>  1     0        1620         1621
        #>  2     0        1620         1622
        #>  3     0        1620         1623
        #>  4     1        1620         1624
        #>  5     0        1622         1623
        #>  6     0        1622         1624
        #>  7     0        1622         1625
        #>  8     0        1622         1626
        #>  9     0        1622         1627
        #> 10     1        1622         1628
        

        reprex package (v2.0.0) 于 2021 年 6 月 11 日创建

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-11-12
          • 2019-08-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-09-22
          • 1970-01-01
          • 2019-12-26
          相关资源
          最近更新 更多