How can I scale the array to a different length while keeping its approximate values ​​in R

I have two arrays with different lengths

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)

How to resize an array valueto make it the same length as the array time, preserving its approximate values?

approx() indicates the length is different.

I want to get an array valuelike

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)

therefore the lengths are equal

UPD

Well, the main goal is to calculate the correlation of v1 from v2, where v1 is inside data.frame v1, t1 and v2 is inside data.frame v2, t2.

the data frames v1, t1 and v2, t2 have different lengths, but , we know that t1 and t2 are equal to the time period, so we can superimpose them.

for t1 we have 1,3,5,7,9, and for t2 we have 1,2,3,4,5,6,7,8,9,10.

, , , , . , v1 v2.

v1 t2.

, , , .

+4
2

xout approx
"xout: 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)
+2

. , , , . , , , , ...

pad <- function(x,y){
    fill <- length(y) - length(x)
    run <- rle(x)
    add <- fill %/% length(run$lengths)
    pad <- diff( c( 0 , as.integer( seq( add , fill , length.out = length(run$lengths) ) ) ) )
    rep(run$values , times = run$lengths+pad)
}
pad(value,time)
[1] 1 1 1 1 1 4 4 4 4 4 1 1 1 1 1

, ,

value <- 1:2
time <- 1:10
pad(value,time)
[1] 1 1 1 1 1 2 2 2 2 2
+2

Source: https://habr.com/ru/post/1526721/


All Articles