Ale*_*kin 5 arrays resize r scale rescale
我有两个长度不同的数组
value <- c(1,1,1,4,4,4,1,1,1)
time <- c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15)
Run Code Online (Sandbox Code Playgroud)
如何调整value数组大小以使其长度与time数组相同,并保存其近似值?
approx()函数告诉我们长度不同。
我想让value数组像
value <- c(1,1,1,1,1,4,4,4,4,4,4,1,1,1,1)
time <- c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15)
Run Code Online (Sandbox Code Playgroud)
所以长度相等
UPD
好的,主要目标是计算 v1 与 v2 的相关性,其中 v1 在 data.frame v1,t1 内,v2 在 data.frame v2,t2 内。
v1,t1 和 v2,t2 数据帧具有不同的长度,但我们知道 t1 和 t2 的时间段相同,因此我们可以将它们重叠。
对于 t1,我们有 1,3,5,7,9,对于 t2,我们有 1,2,3,4,5,6,7,8,9,10。
问题是两个数据帧是分开但同时记录的,所以我需要缩放其中一个数据帧以覆盖另一个数据帧。然后我可以计算 v1 如何影响 v2 的相关性。
这就是为什么我需要将 v1 缩放到 t2 长度。
对不起,伙计们,我不知道如何用英语正确地写出目标。
您可以使用
“ ”xout中的参数。approxxout: an optional set of numeric values specifying where interpolation is to take place.
# create some fake data, which I _think_ may resemble the data you described in edit.
set.seed(123)
# "for t1 we have 1,3,5,7,9"
df1 <- data.frame(time = c(1, 3, 5, 7, 9), value = sample(1:10, 5))
df1
# "for t2 we have 1,2,3,4,5,6,7,8,9,10", the 'full time series'.
df2 <- data.frame(time = 1:10, value = sample(1:10))
# interpolate using approx and the xout argument
# The time values for 'full time series', df2$time, is used as `xout`.
# default values of arguments (e.g. linear interpolation, no extrapolation)
interpol1 <- with(df1, approx(x = time, y = value, xout = df2$time))
# some arguments you may wish to check
# extrapolation rules
interpol2 <- with(df1, approx(x = time, y = value, xout = df2$time,
rule = 2))
# interpolation method ('last observation carried forward")
interpol3 <- with(df1, approx(x = time, y = value, xout = df2$time,
rule = 2, method = "constant"))
df1
# time value
# 1 1 3
# 2 3 8
# 3 5 4
# 4 7 7
# 5 9 6
interpol1
# $x
# [1] 1 2 3 4 5 6 7 8 9 10
#
# $y
# [1] 3.0 5.5 8.0 6.0 4.0 5.5 7.0 6.5 6.0 NA
interpol3
# $x
# [1] 1 2 3 4 5 6 7 8 9 10
#
# $y
# [1] 3 3 8 8 4 4 7 7 6 6
# correlation between a vector of inter-(extra-)polated values
# and the 'full' time series
cor.test(interpol3$y, df2$value)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
745 次 |
| 最近记录: |