在R

时间:2016-10-05 20:42:26

标签: r function

我正在研究R中的公式,它反向迭代数据帧。现在,公式将采用一定数量的列,并找到每列的平均值,直到设置的行数。我想做的是对于for循环的每次迭代,行号减少1。这里的目标是创建一个“三角形”引用,每次迭代使用一个较少的值作为列均值。

这里有一些代码可用于创建在公式中有效的样本数据。

test = data.frame(p1 = c(1,2,0,1,0,2,0,1,0,0), p2 = c(0,0,1,2,0,1,2,1,0,1))

这是我正在使用的功能。我最好的猜测是,我需要在i部分中添加mean(data[1:row, i])的某种引用,但我似乎无法自行处理逻辑/数学。

averagePickup = function(data, day, periods) {
  # data will be your Pickup Data
  # day is the day you're forecasting for (think row number)
  # periods is the period or range of periods that you need to average (a column or range of columns).
   pStart = ncol(data)
   pEnd = ncol(data) - (periods-1)
   row = (day-1)
   new_frame <- as.data.frame(matrix(nrow = 1, ncol = periods))

  for(i in pStart:pEnd) {
    new_frame[1,1+abs(ncol(data)-i)] <- mean(data[1:row , i])
  }
  return(sum(new_frame[1,1:ncol(new_frame)]))
}

现在,输入averagePickup(test,5,2)将产生1.75的结果。这是两列前4个值的平均值之和。我想要的结果是1.33333。这将是p1列中前4个值的平均值与p2列中前3个值的平均值之和。

如果您需要进一步澄清,请告诉我,我仍然是R!

2 个答案:

答案 0 :(得分:0)

喜欢这个吗?

test = data.frame(p1 = c(1,2,0,1,0,2,0,1,0,0), p2 = c(0,0,1,2,0,1,2,1,0,1))
averagePickup = function(data, first, second) {
  return(mean(test[1:first,1]) + mean(test[1:second,2]))
}

averagePickup(test,4,3)

这给你1.333333

答案 1 :(得分:0)

Welp,我最后还是用几块头砸墙来搞清楚了。这对我有用:

averagePickup = function(data, day, periods) {
  # data will be your Pickup Data
  # day is the day you're forecasting for (think row number)
  # periods is the period or range of periods that you need to average (a column or range of columns).
  pStart = ncol(data)
  pEnd = ncol(data) - (periods-1)
  row = (day-1)
  new_frame <- as.data.frame(matrix(nrow = 1, ncol = periods))

  q <- 0 # Instantiated a q value. Run 0 will be the first one.
  for(i in pStart:pEnd) {
    new_frame[1,1+abs(ncol(data)-i)] <- mean(data[1:(day - periods + q) , i]) # Added a subtraction of q from the row number to use.
    q <- q + 1 # Incrementing q, so the next time will use one less row.
  }
   return(sum(new_frame[1,1:ncol(new_frame)]))
}