Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I scale an array to another length saving it's 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 can I resize the value array to make it same length as the time array, saving it's approximate values ?

approx() function tells that lengths are differ.

I want to get value array to be like

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)

so lengths are equal

UPD

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

the v1,t1 and v2,t2 data frames have different lengths, but we know that t1 and t2 is for equal time period so we can overlay 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.

The problem is that two data frames are recorded separately but simultaneusly so I need to scale one of them to overlay another data.frame. And then I can calculate correlation of how v1 affects on v2.

That why I need to scale v1 to t2 length.

I'm sorry guys, I dont know how to write the goal correctly in english.

like image 637
Alexander.Iljushkin Avatar asked Feb 13 '14 09:02

Alexander.Iljushkin


1 Answers

You may use the xout argument in 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)
like image 156
Henrik Avatar answered Oct 11 '22 15:10

Henrik