从连续变量创建虚拟分位数变量

时间:2020-02-26 10:03:43

标签: r dummy-variable continuous

以下是我正在使用的数据:

df_out = pd.DataFrame({
    'A': [1, 1, 1, 2, 3, 3],
    'B1': [1, 4, 7, 10, 13, 16],
    'B2': [2, 5, 8, 11, 14, 17],
    'B3': [3, 6, 9, 12, 15, 18],
    'C': ['a', 'a', 'a', 'b', 'c', 'c']
})

df_out
    A   B1  B2  B3  C
0   1   1   2   3   a
1   1   4   5   6   a
2   1   7   8   9   a
3   2   10  11  12  b
4   3   13  14  15  c
5   3   16  17  18  c

我想为x <- getURL("https://raw.githubusercontent.com/dothemathonthatone/maps/master/testmain.csv") data <- read.csv(text = x) 中的上,中和下三分之一值创建一个虚拟变量。我的id列year_hh_inc中的每个值都可能包含reg_schl的多个值,因此虚拟变量需要分组在year_hh_inc上。 我希望能够区分每个唯一reg_schlyear_hh_inc中的值。

到目前为止,我有以下内容作为Sotos的解决方案发布在下面:

reg_schl

这很好。

我也使用了艾伦(Allan)提供的解决方案:

data %>% 
 group_by(reg_schl) %>%
 mutate(category = cut(year_hh_inc, breaks = (quantile(year_hh_inc, c(0, 1 / 3, 2 / 3, 1), na.rm = TRUE)), labels = c("low", "middle", "high"), include.lowest = TRUE), vals = 1) %>% 
 pivot_wider(names_from = category, values_from = vals, values_fill = list(vals = 0))

2 个答案:

答案 0 :(得分:2)

您可以使用split-lapply-rbind范例:

cut_by_id <- function(x)
{
  x$category <- cut(x$inc, quantile(x$inc, c(0,1/3,2/3,1), na.rm = TRUE), 
                    labels = c("low","middle","high"), include.lowest = TRUE)
  return(x)
}

data <- do.call(rbind, lapply(split(data, data$id), cut_by_id))

data
#>      id   inc fee fert fee_per_inc category
#> 1.1   1 11000 125 0.15 0.011363636      low
#> 1.2   1 15000 150 0.12 0.010000000      low
#> 1.3   1 17000 175 0.22 0.010294118   middle
#> 1.4   1 19000 200 0.13 0.010526316     high
#> 1.5   1 21000 225 0.12 0.010714286     high
#> 2.6   2 13000  55 0.11 0.004230769      low
#> 2.7   2 16000  75 0.09 0.004687500      low
#> 2.8   2 19000  85 0.23 0.004473684   middle
#> 2.9   2 21000  95 0.05 0.004523810     high
#> 2.10  2 25000 105 0.01 0.004200000     high
#> 3.11  3 18000  75 0.25 0.004166667      low
#> 3.12  3 21000  85 0.03 0.004047619      low
#> 3.13  3 23000  95 0.05 0.004130435   middle
#> 3.14  3 27000 105 0.15 0.003888889     high
#> 3.15  3 30000 115 0.25 0.003833333     high

box  <- boxplot(data$inc ~ data$category, col = 3:5)

reprex package(v0.3.0)于2020-02-26创建

答案 1 :(得分:1)

我们可以根据分位数创建您的因子变量,并分散这些值,即

library(dplyr)
library(tidyr)

data %>% 
 group_by(id) %>%
 mutate(category = cut(inc, breaks = (quantile(inc, c(0, 1 / 3, 2 / 3, 1), na.rm = TRUE)), labels = c("low", "middle", "high"), include.lowest = TRUE), vals = 1) %>% 
 pivot_wider(names_from = category, values_from = vals, values_fill = list(vals = 0))

给出,

# A tibble: 15 x 8
# Groups:   id [3]
      id   inc   fee  fert fee_per_inc   low middle  high
   <dbl> <dbl> <dbl> <dbl>       <dbl> <dbl>  <dbl> <dbl>
 1     1 11000   125  0.15     0.0114      1      0     0
 2     1 15000   150  0.12     0.01        1      0     0
 3     1 17000   175  0.22     0.0103      0      1     0
 4     1 19000   200  0.13     0.0105      0      0     1
 5     1 21000   225  0.12     0.0107      0      0     1
 6     2 13000    55  0.11     0.00423     1      0     0
 7     2 16000    75  0.09     0.00469     1      0     0
 8     2 19000    85  0.23     0.00447     0      1     0
 9     2 21000    95  0.05     0.00452     0      0     1
10     2 25000   105  0.01     0.0042      0      0     1
11     3 18000    75  0.25     0.00417     1      0     0
12     3 21000    85  0.03     0.00405     1      0     0
13     3 23000    95  0.05     0.00413     0      1     0
14     3 27000   105  0.15     0.00389     0      0     1
15     3 30000   115  0.25     0.00383     0      0     1

注意我在include.lowest = TRUE中添加了参数cut,以便捕获第一个标签(low)中的最小值