Reputation: 31
I wrote a function, generate supply, which generates the delivery of some product to the store.That is, the function works by creating a text file with two columns: the day of the month (or week, decade) and the value of the product, using a random number generator so that the values of the products on different days are different. Function parameters: file name, location, maximum and minimum value,number of days. The problem is that I use this function to create data for deliveries and sales to the store, and my sales exceed deliveries. I tried to configure it somehow through the maximum and minimum parameters, but it didn't work. How could I solve this problem? Here is code:
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
Upvotes: 0
Views: 79
Reputation: 16978
One quick and ugly solution. You function has to know the maximum value per day.
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)
}
The modifies function checks if delivery is provided as input is.null(deliver)
. If deliver
is present, it calculates the minimum per good and day, so sales couldn't exeed the deliveries. For this calculation the data.frame is transformed into a matrix and we use minimum(x,y) = (x + y - | x - y |) / 2
.
But be careful: this works if and only if your data.frame contains numbers.
So for calculation the sales you use the delivery as addtional input which defaults to 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
Upvotes: 2