R 基于比较其他两个向量元素的新向量“;滞后;?

R 基于比较其他两个向量元素的新向量“;滞后;?,r,R,我有两个向量,subject和target。我想基于两个现有向量之间的比较创建一个新的向量,并对元素进行比较滞后。我已经用下面的循环解决了这个问题,但我想知道是否有更优雅的解决方案使用apply subject <- c(200, 195, 190, 185, 185, 185, 188, 189, 195, 200, 210, 210) target <- c(subject[1], subject[1]-cumsum(rep(perweek, length(subject)-1)

我有两个向量,
subject
target
。我想基于两个现有向量之间的比较创建一个新的向量,并对元素进行比较
滞后
。我已经用下面的循环解决了这个问题,但我想知道是否有更优雅的解决方案使用
apply

subject <- c(200, 195, 190, 185, 185, 185, 188, 189, 195, 200, 210, 210)
target <- c(subject[1], subject[1]-cumsum(rep(perweek, length(subject)-1)))
adjtarget <- target                                               

for (i in 1:(length(subject)-1)) {
  if (subject[i] > adjtarget[i]) {                
    adjtarget[i+1] <- adjtarget[i]           
   } else {                                       
    adjtarget[i+1] <- adjtarget[i]-perweek  }
   }
 }

subject这并不能完全解决您的问题,但可能指向一个有用的方向。我忽略了更改
adjtarget
和比较它之间的相互作用,并展示了一个类似的问题,我们将其与常量
target
进行比较。然后可以将循环中的
if
更改为向量比较:

lv <- but.last(subject) > but.last(target)
ind <- which(lv)
或者

x <- c(target[1], but.last(target) - (!lv)*perweek

x只是澄清一下,如果我理解你的代码,这就是你想要的结果

> (goal <- cbind(subject,target,adjtarget))

      subject target adjtarget
 [1,]     200    200       200
 [2,]     195    198       198
 [3,]     190    196       196
 [4,]     185    194       194
 [5,]     185    192       192
 [6,]     185    190       190
 [7,]     188    188       188
 [8,]     189    186       186
 [9,]     195    184       186
[10,]     200    182       186
[11,]     210    180       186
[12,]     210    178       186
>(目标y目标,1,0)#符合真实情况
>x x[ind+1]cbind(目标,x,y)
主题目标调整目标x y
[1,]     200    200       200 200 0
[2,]     195    198       198 198 0
[3,]     190    196       196 196 0
[4,]     185    194       194 194 0
[5,]     185    192       192 192 0
[6,]     185    190       190 190 0
[7,]     188    188       188 188 0
[8,]     189    186       186 186 1
[9,]1951841861#分配正确(?)
[10,]200 182 186 184 1#不正确的x;应该是186
[11,]210 180 186 182 1#x不正确;应该是186
[12,]210 178 186 180 1#不正确的x;应该是186

perweek变量的值是多少?很抱歉,我忘了这一点:perweek只是一个整数。在我的例子中,每周代码都有点难理解。在循环中,赋值在下一次迭代中将要比较的位置发生变化。恐怕没有一个优雅的解决方案可以用R来表达这一点,在R中,你通常并行处理整个向量。比较是一个移动的目标
> (goal <- cbind(subject,target,adjtarget))

      subject target adjtarget
 [1,]     200    200       200
 [2,]     195    198       198
 [3,]     190    196       196
 [4,]     185    194       194
 [5,]     185    192       192
 [6,]     185    190       190
 [7,]     188    188       188
 [8,]     189    186       186
 [9,]     195    184       186
[10,]     200    182       186
[11,]     210    180       186
[12,]     210    178       186
> y <- ifelse(subject > target, 1, 0) # matches TRUE case
> x <- target
> x[ind+1] <- target[ind]
> cbind(goal, x, y)
      subject target adjtarget   x y
 [1,]     200    200       200 200 0
 [2,]     195    198       198 198 0
 [3,]     190    196       196 196 0
 [4,]     185    194       194 194 0
 [5,]     185    192       192 192 0
 [6,]     185    190       190 190 0
 [7,]     188    188       188 188 0
 [8,]     189    186       186 186 1
 [9,]     195    184       186 186 1 # assigned correctly (?)
[10,]     200    182       186 184 1 # incorrect x; should be 186
[11,]     210    180       186 182 1 # incorrect x; should be 186
[12,]     210    178       186 180 1 # incorrect x; should be 186