【问题标题】:How to know if a number is in a determinated interval in R如何知道一个数字是否在R中的确定区间内
【发布时间】:2019-11-26 15:11:10
【问题描述】:

我有一个包含 3 列的数据集:默认值、高度和重量。

我对变量进行了分箱,并在一个列表中将其(我必须这样做)。每个分箱都有一个相关的问题,但现在我想将这些问题放在原始数据框中,具体取决于我观察到的存储桶: 比如数据框

df1 <- data.frame(default=sample(c(0,1), replace=TRUE, size=100, prob=c(0.9,0.1)),
                  height=sample(150:180, 100, replace=T),
                  weight=sample(50:80,100,replace=T))
> head(df1)
#    default  height  weight
# 1       0    172     54
# 2       0    169     71
# 3       0    164     61
# 4       0    156     55
# 5       0    180     66
# 6       0    162     63

垃圾箱(我只展示第一个)

bins <- lapply(c("height","weight"), function(x) woe.binning(df1, "default", x,
                                                 min.perc.total=0.05,
                                                 min.perc.class=0.05,event.class=1,
                                                 stop.limit = 0.05)[2])
# [[1]]
# [[1]][[1]]
#                woe cutpoints.final cutpoints.final[-1] iv.total.final  0 1 col.perc.a col.perc.b      iv.bins
# (-Inf,156] -46.58742            -Inf                 156      0.1050725 21 5 0.24137931 0.38461538 0.0667299967
# (156,168]   23.91074             156                 168      0.1050725 34 4 0.39080460 0.30769231 0.0198727638
# (168,169]  -10.91993             168                 169      0.1050725  6 1 0.06896552 0.07692308 0.0008689599
# (169, Inf]  25.85255             169                 Inf      0.1050725 26 3 0.29885057 0.23076923 0.0176007627
# Missing           NA             Inf             Missing      0.1050725  0 0 0.00000000 0.00000000           

现在我想用 bins 查看我的数据。 我想要的输出与此类似

#    default  height  weight woe_height   woe_weight
# 1       0    160     54      23.91074   -8.180032
# 2       0    140     71     -46.58742   -7.640947 

有什么办法吗?我在这里看到的主要问题是间隔 (a,b) 是strings。我正在考虑使用substr() 或类似的东西来分隔逻辑选项中的字符串,但我认为这行不通,而且它不是很优雅。 欢迎任何帮助,在此先感谢。

【问题讨论】:

    标签: r intervals binning


    【解决方案1】:

    这对你有用吗?

    apply_woe_binning <- function(df, x){
    
      # woe binning
      w <- woe.binning(df, "default", x,
                       min.perc.total=0.05,
                       min.perc.class=0.05,
                       event.class=1,
                       stop.limit = 0.05)[[2]]
    
      # create new column name
      new_col <- paste("woe", x, sep = "_")
    
      # define cuts
      cuts <- cut(df[[x]], w$cutpoints.final)
    
      # add new column
      df[[new_col]] <- w[cuts, "woe", drop = TRUE]
    
      df
    }
    
    # one by one
    df2 <- apply_woe_binning(df1, "height")
    df2 <- apply_woe_binning(df2, "weight")
    
    
    # in a functional
    df2 <- Reduce(function(y, x) apply_woe_binning(df = y, x = x), 
                  c("height","weight"),
                  init = df1)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多