Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 从连续变量创建虚拟分位数变量_R_Dummy Variable_Continuous - Fatal编程技术网

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

R 从连续变量创建虚拟分位数变量,r,dummy-variable,continuous,R,Dummy Variable,Continuous,以下是我正在处理的数据: x <- getURL("https://raw.githubusercontent.com/dothemathonthatone/maps/master/testmain.csv") data <- read.csv(text = x) 这很有效 我还使用了Allan提供的解决方案: cut_by_id <- function(x) { x$category <- cut(x$year_hh_inc, quantile(x$yea

以下是我正在处理的数据:

x <- getURL("https://raw.githubusercontent.com/dothemathonthatone/maps/master/testmain.csv")
    data <- read.csv(text = x)
这很有效

我还使用了Allan提供的解决方案:

cut_by_id <- function(x)
{
  x$category <- cut(x$year_hh_inc, quantile(x$year_hh_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))

cut\u by\u id您可以使用
split
-
lappy
-
rbind
范例:

cut_by_id 1.21 15000 150 0.12 0.010000000低
#>1.3 1 17000 175 0.22 0.010294118中
#>1.4 1 19000 200 0.13 0.010526316高
#>1.5 1 21000 225 0.12 0.010714286偏高
#>2.6 2 13000 55 0.11 0.004230769低
#>2.7 2 16000 75 0.09 0.004687500低
#>2.8 2 19000 85 0.23 0.004473684中
#>2.9 2 21000 95 0.05 0.004523810偏高
#>2.10 2 25000 105 0.01 0.004200000高
#>3.11 3 18000 75 0.25 0.004166667低
#>3.12 3 21000 85 0.03 0.004047619低
#>3.13 3 23000 95 0.05 0.004130435中
#>3.14 3 27000 105 0.15 0.00388889偏高
#>3.15 3 30000 115 0.25 0.003833333偏高

框您可以使用
split
-
lappy
-
rbind
范例:

cut_by_id 1.21 15000 150 0.12 0.010000000低
#>1.3 1 17000 175 0.22 0.010294118中
#>1.4 1 19000 200 0.13 0.010526316高
#>1.5 1 21000 225 0.12 0.010714286偏高
#>2.6 2 13000 55 0.11 0.004230769低
#>2.7 2 16000 75 0.09 0.004687500低
#>2.8 2 19000 85 0.23 0.004473684中
#>2.9 2 21000 95 0.05 0.004523810偏高
#>2.10 2 25000 105 0.01 0.004200000高
#>3.11 3 18000 75 0.25 0.004166667低
#>3.12 3 21000 85 0.03 0.004047619低
#>3.13 3 23000 95 0.05 0.004130435中
#>3.14 3 27000 105 0.15 0.00388889偏高
#>3.15 3 30000 115 0.25 0.003833333偏高

box我们可以基于分位数创建因子变量,并传播这些值,即

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))
这就给了,

#一个tible:15 x 8
#组别:id[3]
id inc费用fert费用per_inc低-中-高
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

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

我们可以基于分位数创建因子变量并传播这些值,即

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))
这就给了,

#一个tible:15 x 8
#组别:id[3]
id inc费用fert费用per_inc低-中-高
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

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

请显示您的预期输出好吗?请显示您的预期输出好吗?谢谢您的回答。我现在正在实施它,我有一个关于
x$category@MichaelPerdue抱歉-那应该是
inc
有没有办法改变代码(在上面的例子中),使其不按
id
分组,也就是说,如果您删除
标签=
它应该用数字范围标记组,则它在箱线图中显示
inc
的范围,与
id
@MichaelPerdue无关。如果您愿意,您可以设置自己的中断,而不是使用分位数,然后根据这些中断标记它们。我可能会看到分位数是什么,然后选择附近的整数作为断点,然后用这些数字作为标签。谢谢你的回答。我现在正在实施它,我有一个关于
x$category@MichaelPerdue抱歉-那应该是
inc
有没有办法改变代码(在上面的例子中),使其不按
id
分组,也就是说,如果您删除
标签=
它应该用数字范围标记组,则它在箱线图中显示
inc
的范围,与
id
@MichaelPerdue无关。如果您愿意,您可以设置自己的中断,而不是使用分位数,然后根据这些中断标记它们。我可能会看到分位数是什么,然后选择附近的整数作为断点,然后用这些数字作为标签。谢谢你的回答。检查表时,我发现
inc
id==1
的值都没有标记在
high
列中。我正在努力
# 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