Subset data with dynamic conditions in R

纵然是瞬间 提交于 2019-12-01 23:07:36

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!