了解此示例中的 R 语法
Understanding R syntax in this example
Finding Peak in a dataset - using R
嗨
我在 stackexchange 上看到了这个帖子。我还不是 R 程序员。但是我想在C中实现。但是不熟悉R语法,我无法理解代码。我知道它创建数组,例如 y.max 和 i.max 但我不确定完成的操作以及它如何操作数组。下面是我特别感兴趣的四行
y.max <- rollapply(zoo(y.smooth), 2*w+1, max, align="center")
delta <- y.max - y.smooth[-c(1:w, n+1-1:w)]
i.max <- which(delta <= 0) + w
list(x=x[i.max], i=i.max, y.hat=y.smooth)
一些有助于理解这些特定语法的指针。
这是该代码的翻译。 R 经常使用嵌套的函数调用,如果您不知道每个函数的作用,就很难理解。为了解决这个问题,我将一些行分成多行并将结果存储在新变量中。
# convert y.smooth to a zoo (time series) object
zoo_y.smooth <- zoo(y.smooth)
# divide the data into rolling windows of width 2*w+1
# get the max of each window
# align = "center" makes the indices of y.max be aligned to the center
# of the windows
y.max <- rollapply(zoo_y.smooth,
width = 2*w+1,
FUN = max,
align="center")
R 子集化可以非常简洁。 c(1:w, n+1-1:w)
创建一个名为 toExclude
的数字向量。将带有 -
的向量传递给子集运算符 []
会选择 y.smooth
的所有元素,但 toExclude
中指定索引处的元素除外。省略 -
会适得其反。
# select all of the elements of y.smooth except 1 to w and n+1-1 to w
toExclude <- c(1:w, n+1-1:w)
subset_y.smooth <- y.smooth[-toExclude]
# element-wise subtraction
delta <- y.max - subset_y.smooth
# logical vector the same length of delta indicating which elements
# are less than or equal to 0
nonPositiveDelta <- delta <= 0
所以nonPositiveDelta
是一个类似TRUE FALSE FALSE...的向量,delta的每个元素都有一个元素,表示delta的哪些元素是非正的。
# vector containing the index of each element of delta that's <= 0
indicesOfNonPositiveDeltas <- which(nonPositiveDelta)
另一方面,indicesOfNonPositiveDeltas
是一个类似于 1、3、4、5、8 的向量,其中包含前一个为 TRUE 的向量的每个元素的索引。
# indices plus w
i.max <- indicesOfNonPositiveDeltas + w
最后,将结果存储在一个列表中。列表有点像数组的数组,其中列表的每个元素本身可以是另一个列表或任何其他类型。在这种情况下,列表的每个元素都是一个向量。
# create a three element list
# each element is named, with the name to the left of the equal sign
list(
x=x[i.max], # the elements of x at indices specified by i.max
i=i.max, # the indices of i.max
y.hat=y.smooth) # the y.smooth data
在没有看到其余代码或对它应该做什么的描述的情况下,我不得不猜测一下,但希望这对您有所帮助。
Finding Peak in a dataset - using R
嗨
我在 stackexchange 上看到了这个帖子。我还不是 R 程序员。但是我想在C中实现。但是不熟悉R语法,我无法理解代码。我知道它创建数组,例如 y.max 和 i.max 但我不确定完成的操作以及它如何操作数组。下面是我特别感兴趣的四行
y.max <- rollapply(zoo(y.smooth), 2*w+1, max, align="center")
delta <- y.max - y.smooth[-c(1:w, n+1-1:w)]
i.max <- which(delta <= 0) + w
list(x=x[i.max], i=i.max, y.hat=y.smooth)
一些有助于理解这些特定语法的指针。
这是该代码的翻译。 R 经常使用嵌套的函数调用,如果您不知道每个函数的作用,就很难理解。为了解决这个问题,我将一些行分成多行并将结果存储在新变量中。
# convert y.smooth to a zoo (time series) object
zoo_y.smooth <- zoo(y.smooth)
# divide the data into rolling windows of width 2*w+1
# get the max of each window
# align = "center" makes the indices of y.max be aligned to the center
# of the windows
y.max <- rollapply(zoo_y.smooth,
width = 2*w+1,
FUN = max,
align="center")
R 子集化可以非常简洁。 c(1:w, n+1-1:w)
创建一个名为 toExclude
的数字向量。将带有 -
的向量传递给子集运算符 []
会选择 y.smooth
的所有元素,但 toExclude
中指定索引处的元素除外。省略 -
会适得其反。
# select all of the elements of y.smooth except 1 to w and n+1-1 to w
toExclude <- c(1:w, n+1-1:w)
subset_y.smooth <- y.smooth[-toExclude]
# element-wise subtraction
delta <- y.max - subset_y.smooth
# logical vector the same length of delta indicating which elements
# are less than or equal to 0
nonPositiveDelta <- delta <= 0
所以nonPositiveDelta
是一个类似TRUE FALSE FALSE...的向量,delta的每个元素都有一个元素,表示delta的哪些元素是非正的。
# vector containing the index of each element of delta that's <= 0
indicesOfNonPositiveDeltas <- which(nonPositiveDelta)
另一方面,indicesOfNonPositiveDeltas
是一个类似于 1、3、4、5、8 的向量,其中包含前一个为 TRUE 的向量的每个元素的索引。
# indices plus w
i.max <- indicesOfNonPositiveDeltas + w
最后,将结果存储在一个列表中。列表有点像数组的数组,其中列表的每个元素本身可以是另一个列表或任何其他类型。在这种情况下,列表的每个元素都是一个向量。
# create a three element list
# each element is named, with the name to the left of the equal sign
list(
x=x[i.max], # the elements of x at indices specified by i.max
i=i.max, # the indices of i.max
y.hat=y.smooth) # the y.smooth data
在没有看到其余代码或对它应该做什么的描述的情况下,我不得不猜测一下,但希望这对您有所帮助。