R 求向量中连续增加k倍的分量

R 求向量中连续增加k倍的分量,r,R,我想创建一个函数,它可以找到向量的分量,这些分量持续增加k倍 也就是说,如果设计的函数是f(x,k)和x=c(2,3,4,3,5,6,5,7),那么 f(x,1)的值是2,3,3,5,5,因为只有x的这些分量增加了1倍 此外,如果k=2,则f(x,2)的值为2,3,因为只有这些分量连续增加2倍。(2→3.→4和3→5.→(六) 我想我应该使用重复的语法,比如,来达到这个目的。我不太理解你问题的第二部分(k=2),但是对于第一部分,你可以使用如下内容: test<-c(2,3,4,3,5,6

我想创建一个函数,它可以找到向量的分量,这些分量持续增加k倍

也就是说,如果设计的函数是
f(x,k)
x=c(2,3,4,3,5,6,5,7)
,那么
f(x,1)
的值是
2,3,3,5,5
,因为只有
x
的这些分量增加了1倍

此外,如果
k=2
,则
f(x,2)
的值为
2,3
,因为只有这些分量连续增加2倍。(2→3.→4和3→5.→(六)


我想我应该使用重复的语法,比如
,来达到这个目的。

我不太理解你问题的第二部分(k=2),但是对于第一部分,你可以使用如下内容:

test<-c(2,3,4,3,5,6,5,7) #Your vector

diff(test) #Differentiates the vector
diff(test)>0 #Turns the vector in a logical vector with criterion >0

test[diff(test)>0] #Returns only the elements of test that correspond to a TRUE value in the previous line
test0#使用条件>0将向量转换为逻辑向量
test[diff(test)>0]#只返回与前一行中的真值相对应的test元素

我不太理解你问题的第二部分(k=2),但对于第一部分,你可以使用如下内容:

test<-c(2,3,4,3,5,6,5,7) #Your vector

diff(test) #Differentiates the vector
diff(test)>0 #Turns the vector in a logical vector with criterion >0

test[diff(test)>0] #Returns only the elements of test that correspond to a TRUE value in the previous line
test0#使用条件>0将向量转换为逻辑向量
test[diff(test)>0]#只返回与前一行中的真值相对应的test元素

我不太理解你问题的第二部分(k=2),但对于第一部分,你可以使用如下内容:

test<-c(2,3,4,3,5,6,5,7) #Your vector

diff(test) #Differentiates the vector
diff(test)>0 #Turns the vector in a logical vector with criterion >0

test[diff(test)>0] #Returns only the elements of test that correspond to a TRUE value in the previous line
test0#使用条件>0将向量转换为逻辑向量
test[diff(test)>0]#只返回与前一行中的真值相对应的test元素

我不太理解你问题的第二部分(k=2),但对于第一部分,你可以使用如下内容:

test<-c(2,3,4,3,5,6,5,7) #Your vector

diff(test) #Differentiates the vector
diff(test)>0 #Turns the vector in a logical vector with criterion >0

test[diff(test)>0] #Returns only the elements of test that correspond to a TRUE value in the previous line
test0#使用条件>0将向量转换为逻辑向量
test[diff(test)>0]#只返回与前一行中的真值相对应的test元素
1)使用zoo软件包中的
rollapply

library(zoo)
f <- function(x, k)
       x[rollapply(x, k+1, function(x) all(diff(x) > 0), align = "left", fill = FALSE)]
1a)此变化略短,也适用于:

f2 <- function(x, k) head(x, -k)[ rollapply(diff(x) > 0, k, all) ]
f2 0,k,all)]
2)这里是1a的一个版本,它不使用任何软件包:

f3 <- function(x, k) head(x, -k)[ apply(embed(diff(x) > 0, k), 1, all) ]
f3 0,k),1,全部]
1)使用zoo软件包中的
rollapply

library(zoo)
f <- function(x, k)
       x[rollapply(x, k+1, function(x) all(diff(x) > 0), align = "left", fill = FALSE)]
1a)此变化略短,也适用于:

f2 <- function(x, k) head(x, -k)[ rollapply(diff(x) > 0, k, all) ]
f2 0,k,all)]
2)这里是1a的一个版本,它不使用任何软件包:

f3 <- function(x, k) head(x, -k)[ apply(embed(diff(x) > 0, k), 1, all) ]
f3 0,k),1,全部]
1)使用zoo软件包中的
rollapply

library(zoo)
f <- function(x, k)
       x[rollapply(x, k+1, function(x) all(diff(x) > 0), align = "left", fill = FALSE)]
1a)此变化略短,也适用于:

f2 <- function(x, k) head(x, -k)[ rollapply(diff(x) > 0, k, all) ]
f2 0,k,all)]
2)这里是1a的一个版本,它不使用任何软件包:

f3 <- function(x, k) head(x, -k)[ apply(embed(diff(x) > 0, k), 1, all) ]
f3 0,k),1,全部]
1)使用zoo软件包中的
rollapply

library(zoo)
f <- function(x, k)
       x[rollapply(x, k+1, function(x) all(diff(x) > 0), align = "left", fill = FALSE)]
1a)此变化略短,也适用于:

f2 <- function(x, k) head(x, -k)[ rollapply(diff(x) > 0, k, all) ]
f2 0,k,all)]
2)这里是1a的一个版本,它不使用任何软件包:

f3 <- function(x, k) head(x, -k)[ apply(embed(diff(x) > 0, k), 1, all) ]
f3 0,k),1,全部]

完全矢量化的解决方案:

f <- function(x, k = 1) {

  rlecumsum = function(x)
  { #cumsum with resetting
    #http://stackoverflow.com/a/32524260/1412059
    cs = cumsum(x)
    cs - cummax((x == 0) * cs)
  }

  x[rev(rlecumsum(rev(c(diff(x) > 0, FALSE) ))) >= k]
}

f(x, 1)
#[1] 2 3 3 5 5
f(x, 2)
#[1] 2 3
f(x, 3)
#numeric(0)
f0,FALSE))>=k]
}
f(x,1)
#[1] 2 3 3 5 5
f(x,2)
#[1] 2 3
f(x,3)
#数字(0)

完全矢量化的解决方案:

f <- function(x, k = 1) {

  rlecumsum = function(x)
  { #cumsum with resetting
    #http://stackoverflow.com/a/32524260/1412059
    cs = cumsum(x)
    cs - cummax((x == 0) * cs)
  }

  x[rev(rlecumsum(rev(c(diff(x) > 0, FALSE) ))) >= k]
}

f(x, 1)
#[1] 2 3 3 5 5
f(x, 2)
#[1] 2 3
f(x, 3)
#numeric(0)
f0,FALSE))>=k]
}
f(x,1)
#[1] 2 3 3 5 5
f(x,2)
#[1] 2 3
f(x,3)
#数字(0)

完全矢量化的解决方案:

f <- function(x, k = 1) {

  rlecumsum = function(x)
  { #cumsum with resetting
    #http://stackoverflow.com/a/32524260/1412059
    cs = cumsum(x)
    cs - cummax((x == 0) * cs)
  }

  x[rev(rlecumsum(rev(c(diff(x) > 0, FALSE) ))) >= k]
}

f(x, 1)
#[1] 2 3 3 5 5
f(x, 2)
#[1] 2 3
f(x, 3)
#numeric(0)
f0,FALSE))>=k]
}
f(x,1)
#[1] 2 3 3 5 5
f(x,2)
#[1] 2 3
f(x,3)
#数字(0)

完全矢量化的解决方案:

f <- function(x, k = 1) {

  rlecumsum = function(x)
  { #cumsum with resetting
    #http://stackoverflow.com/a/32524260/1412059
    cs = cumsum(x)
    cs - cummax((x == 0) * cs)
  }

  x[rev(rlecumsum(rev(c(diff(x) > 0, FALSE) ))) >= k]
}

f(x, 1)
#[1] 2 3 3 5 5
f(x, 2)
#[1] 2 3
f(x, 3)
#numeric(0)
f0,FALSE))>=k]
}
f(x,1)
#[1] 2 3 3 5 5
f(x,2)
#[1] 2 3
f(x,3)
#数字(0)

谢谢您的友好回答。我的意思是,函数f(x,k)找到向量x的分量,这些分量单调增加而不停止k次。因此,在上述情况下,f(x,3)为空,因为没有x的分量在不停止三次的情况下增加。在k>=4中,情况是相同的,即NULL。谢谢你,谢谢你友好的回答。我的意思是,函数f(x,k)找到向量x的分量,这些分量单调增加而不停止k次。因此,在上述情况下,f(x,3)为空,因为没有x的分量在不停止三次的情况下增加。在k>=4中,情况是相同的,即NULL。谢谢你,谢谢你友好的回答。我的意思是,函数f(x,k)找到向量x的分量,这些分量单调增加而不停止k次。因此,在上述情况下,f(x,3)为空,因为没有x的分量在不停止三次的情况下增加。在k>=4中,情况是相同的,即NULL。谢谢你,谢谢你友好的回答。我的意思是,函数f(x,k)找到向量x的分量,这些分量单调增加而不停止k次。因此,在上述情况下,f(x,3)为空,因为没有x的分量在不停止三次的情况下增加。在k>=4中,情况是相同的,即NULL。非常感谢。