在 R 中生成数据

Generating data in R

我写了一个函数,generate supply,它生成一些产品到 store.That 的交付,该函数通过创建一个包含两列的文本文件来工作:一个月中的第几天(或周、十年)和乘积的值,使用随机数生成器使不同日期的乘积值不同。函数参数:文件名、位置、最大值和最小值、天数。问题是我使用此功能为商店的交货和销售创建数据,而我的销售额超过了交货。我试图通过最大和最小参数以某种方式配置它,但它没有用。我该如何解决这个问题? 这是代码:

generate.supply<-  function(way='',
                            file.name='Supply',
                            days=7,
                            min=100,
                            max=140,
                            goods='Milk, packaging'){

  #creating a table from a single column with a length of days
  tabl<-data.frame('Day'=1:days)
  #use the colnames()function to create a column header
  #adding columns for each product
  for(i in 1:length(goods)){
    tabl[i+1]<-sample(x=min[i]:max[i], size = days)
    colnames(x=tabl)[i+1]=goods[i]
  }
 #write this table to a file with the extension txt
  write.table(
    x=tabl,
    file= paste0(way,file.name),
    col.names = TRUE,
    row.names = FALSE
  )
 return(tabl) 
}
generate.supply(way = "", file.name= 'Shop1_in_K',days=7,goods = c('Cottage cheese, pcs','Kefir, pcs', 'Sour cream, pcs'),min=c(85,100,110),max=c(110,120,130))# generating delivery
generate.supply(way = "", file.name= 'Shop1_out_K',days=7,goods = c('Cottage cheese, pcs','Kefir, pcs', 'Sour cream, pcs'),min=c(85,100,110),max=c(105,119,125))# generating sales

一个快速而丑陋的解决方案。您的函数必须知道每天的最大值。

generate.supply<-  function(way='',
                            file.name='Supply',
                            days=7,
                            min=100,
                            max=140,
                            goods='Milk, packaging',
                            deliver=NULL){
  
  #creating a table from a single column with a length of days
  tabl<-data.frame('Day'=1:days)
  #use the colnames()function to create a column header
  #adding columns for each product
  for(i in 1:length(goods)){
    tabl[i+1]<-sample(x=min[i]:max[i], size = days)
    colnames(x=tabl)[i+1]=goods[i]
  }
  
  if (!is.null(deliver)) {
    tabl <- as.data.frame((as.matrix(deliver) + as.matrix(tabl) - abs(as.matrix(deliver) - as.matrix(tabl))) / 2)
  }
  
  #write this table to a file with the extension txt
  # write.table(
  #   x=tabl,
  #   file= paste0(way,file.name),
  #   col.names = TRUE,
  #   row.names = FALSE
  # )
  return(tabl)
}

修改函数检查是否将交付作为输入提供 is.null(deliver)。如果 deliver 存在,它会计算每天每件商品的最小值,因此销售额不能超过交货。对于此计算,data.frame 被转换为矩阵,我们使用 minimum(x,y) = (x + y - | x - y |) / 2。 但要小心:当且仅当您的 data.frame 包含数字时,这才有效。

因此,为了计算销售额,您使用送货作为默认为 NULL 的附加输入:

deliver <- generate.supply(way = "", 
                           file.name= 'Shop1_in_K',
                           days=7,goods = c('Cottage cheese, pcs','Kefir, pcs', 'Sour cream, pcs'),
                           min=c(85,100,110),
                           max=c(110,120,130))# generating delivery
sales <- generate.supply(way = "", 
                         file.name= 'Shop1_out_K',
                         days=7,
                         goods = c('Cottage cheese, pcs','Kefir, pcs', 'Sour cream, pcs'),
                         min=c(85,100,110),
                         max=c(105,119,125), 
                         deliver=deliver)# generating sales