Subset data with dynamic conditions in R

前端 未结 1 1211
情书的邮戳
情书的邮戳 2021-01-21 09:01

I have a dataset of 2500 rows which are all bank loans. Each bank loan has an outstanding amount and collateral type. (Real estate, Machine tools.. etc)

I need to draw a

相关标签:
1条回答
  • 2021-01-21 09:17

    This function I made works:

    pick_records <- function(df,size,bal,collat,max.it) {
      i <- 1
      j <- 1
      while ( i == 1 ) {
        s_index <- sample(1:nrow(df) , size)
        print(s_index)
        output <- df[s_index,]
        out_num <- lapply(output,as.numeric)
        tot.col <- sum(as.numeric(out_num$Collateral))
        if (sum(out_num$balance) < (bal*1.1) &
              sum(out_num$balance) > (bal*0.9) &
              all(  table(out_num$Collateral)/size  <= collat)   ) {
          return(output)
          break
        }
        print(j)
        j <- j + 1
        if ( j == max.it+1) {
          print('No solution found')
          break}     
      }
    } 
    
    > a <- pick_records(dataset,5,200000,0.4,20)
    > a
      balance       Collateral
    3   35000    Machine tools
    7    5000 Office equipment
    4   40000    Auto Vehicles
    5   65000      Real estate
    2   50000       Aeroplanes
    

    Where df is your dataframe, size is the number of records you want and max.it the number of maximum iterations to find a solution before returning a no solution found error, bal is the limit for balance and collat the same for Collateral. You can change those as you please.

    Let me know if you don't get any part of it.

    0 讨论(0)
提交回复
热议问题