当前位置: 首页 > 知识库问答 >
问题:

如何通过R中的算术条件为组生成随机值

支铭晨
2023-03-14

我有这种结构的数据集

mydata=structure(list(supps = c("KR", "KR", "KR", "KR", "KR", "KR", 
"KR", "KR", "KR", "KR", "aeroclub", "aeroclub", "aeroclub", "aeroclub", 
"aeroclub", "aeroclub", "aeroclub", "aeroclub", "aeroclub", "aeroclub"
), date = c("01.05.2021", "01.06.2021", "02.05.2021", "02.06.2021", 
"03.05.2021", "03.06.2021", "04.05.2021", "04.06.2021", "05.05.2021", 
"05.06.2021", "01.05.2021", "01.06.2021", "02.05.2021", "02.06.2021", 
"03.05.2021", "03.06.2021", "04.05.2021", "04.06.2021", "05.05.2021", 
"05.06.2021"), turnover = c(0, 0, 32159.00888, 25220.0027, 0, 
0, 245312.682, 189901.1224, 0, 0, 1531959.833, 1591612, 1834696.667, 
1885169, 1871615.167, 1823398, 4891342, 5253701.167, 0, 0), fee = c(0, 
0, 651, 37, 0, 0, 2341, 7548, 0, 0, 40519.5, 30415, 34767.66667, 
39289, 39175.66667, 45798, 94819.5, 116803.1667, 0, 0), comiss = c(0, 
0, 764.81, 537.67, 0, 0, 8578.25, 6198.115, 0, 0, -2023.41, -1941.67, 
-550.82, 1323.23, -1029.47, -638.47, -1034.58, -1332.95, 0, 0
), intencive = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 26.4, 1945.8, 
2199.48, 3740.76, 6499.2, 32188.68, 42337.44, 0, 0)), class = "data.frame", row.names = c(NA, 
-20L))

我需要通过支持列(KR 和 aeroclub)为每组提供 vriables 营业额费用 comissin injective 计算下一个条件的价值。例如,我们采用 KR 和营业额变量。最后 2 个值属于日期 03.06.2021-04.06.2021。如果最近的值大于前一个值,则计算值的总和 189901 0=189901。然后,对于日期 05.06.2021-08.06.2021(4 天)的每个变量生成随机值。这以随机顺序计算了189901总和(2%-10%)。更清楚 对于考试输出(对于营业额变量)

05.06.2021  189901+2%=193699,02
06.06.2021 189901+10%=208891,1
07.06.2021  189901+6%=208891,1
08.06.2021 189901+7%=203194

但有时最后一个值可能是负值。例如组=空中俱乐部。变量comiss,最后2个值03.06.2021-04.06.202104.06.22021值-1332,但在03.06.22022值-632,因此在04.06.202值小于03.06.221值。我们将这些值-1332-632=-1954相加,但我们不加和,我们以随机顺序对1954-(2%-10%)进行子跟踪。因此,对于这组comiss期望的输出

05.06.2021  -1954-2%=-1914,92
06.06.2021 -1954-7%=-1817,22
07.06.2021  -1954-6%=-1836,76
08.06.2021 -1954-8%=-1797,68

我怎样才能改正呢?

共有1个答案

吕宇定
2023-03-14

下面的回答假设了一些问题中不完全清楚的事情:

  1. 计算从第3列开始到最后一列
  2. 当有0时,保持为0。不添加随机%。如果你愿意的话,你可以改变
  3. 很少有两个连续值具有不同符号的情况。对于这些情况,在问题之后应用最新值的规则
#storing the unique category of supps
col_supps <- unique(mydata$supps)
#storing the columns for which the calculations will be done
col_names <- colnames(mydata)[3:ncol(mydata)]

#the data frame which will contain the output
output_df <- data.frame()
#Iterating over different supps values

for (x in col_supps) {
#storing one type of supps in a temporary data frame
  mydata %>%
    filter(supps %in% x)-> temp
  temp1<- temp

#temp will act as a reference frame, in temp1 values will be updated
  
#Now, iterating over columns which we need
  
  for (y in col_names) {
    i<- 1
#with while loop, we will iterate over each elememnt of the column and save the result in temp1
    while (i<=(nrow(temp)-1)) {
      if(temp[i+1,y]>0 & temp[i+1,y]>=temp[i,y]){
        temp1[i+1,y] <- (temp[i,y]+temp[i+1,y]) * (runif(1,1.02,1.1))
      }else if(temp[i+1,y]<0 & temp[i+1,y]<=temp[i,y]){
        temp1[i+1,y] <- (temp[i,y]+temp[i+1,y]) * (runif(1,1.02,1.1))
      }else if(temp[i+1,y]>0 & temp[i+1,y]<temp[i,y]){
        temp1[i+1,y] <- (temp[i+1,y]) * (runif(1,1.02,1.1))
      }else if(temp[i+1,y]<0 & temp[i+1,y]>temp[i,y]){
        temp1[i+1,y] <- (temp[i+1,y]) * (runif(1,1.02,1.1))
      }else if(temp[i+1,y]==0){
        temp[i+1,y] <- 0
      }
      i <- i+1
    }
  }
#saving the output in the output data frame before repeating the process for another type of supps
  output_df %>%
    bind_rows(temp1) -> output_df
  
}

现在< code>output_df将得到您想要的最终输出。如果希望随机值具有再现性,可以在< code>while循环下< code>set.seed()。如果这不是我们想要的,那么你可以继续下去。

 类似资料:
  • 问题内容: 我知道普通整数,但是有索引这样的东西吗? 问题答案: 你什么意思?数组索引是普通数,因此您可以轻松地执行 还是说随机的Iterator类?Google是您的朋友,这是我的第一个热门。

  • C++11引入了比C的优越得多的随机数库。在C中,您经常会看到以下代码: 因为以秒为单位返回当前时间,所以对程序的快速连续调用将产生相同的数字序列。解决这一问题的快速方法是在纳秒内提供一个种子: 在C++11中,我所知道的产生好随机数的最短程序是: 是不可移植的,不鼓励使用,因为它可能会选择较差的引擎,如。事实上,不推荐使用,因此首选。通常,我看到人们说用chrono来提供一个种子来代替: 这不仅

  • 我想从randomInts(int-num,int-start,int-end)中获取随机数,并将这些数放入数组。 我试着做

  • 问题内容: 有没有一种方法可以在SQL Server中生成具有定义的字符数的 随机 base36标识符? 我已经搜索并找到了许多将base 36转换为int,反之亦然的示例,但不是随机生成唯一ID的示例。 问题答案: 该解决方案有点冗长,但可以正常使用,并且可以轻松地适应各种需求。这是一些示例输出: 请注意,您需要创建一个视图来包装UDF内不允许使用的RAND。因此,此解决方案需要两个数据库对象,

  • 问题内容: 我有一个像这样的数组: 现在,我想按某种条件过滤该数组,只保留值等于2的元素,并删除值不等于2的所有元素。 所以我的预期结果数组将是: 注意:我想保留原始数组中的键。 如何使用PHP做到这一点?有内置功能吗? 问题答案:

  • 问题内容: 我需要生成介于0(含)到n(不含)之间的任意大随机整数。我最初的想法是调用nextDoublen并乘以n,但是一旦n变得大于2 53,结果将不再均匀分布。 BigInteger 具有以下可用的构造函数: public BigInteger(int numBits, Random rnd) 构造一个随机生成的BigInteger,该整数均匀地分布在0到(2 numBits -1)(包括0