Home > OS >  R Upsampling a time series in a dataframe filling missing values
R Upsampling a time series in a dataframe filling missing values

Time:12-27

I have collected data from two instruments, one is collected @10 Hz and the other is @100Hz. I would like to increase the data from 10Hz to 100Hz in one dataframe to then align and merge the two dataframes together

The example data frame is:

DeltaT Speed Acc HR Player
48860,7 0,03 -0,05 0 Player1
48860,8 0,02 -0,05 0 Player1
48860,9 0,02 -0,04 0 Player1
48861,0 0,02 -0,03 0 Player1
48861,1 0,01 -0,02 0 Player1

Is there a package function that can help me create data between two points?

CodePudding user response:

Manually with the approx function:

dt<- read.table(text=gsub(",", ".", 'DeltaT     Speed   Acc     HR  Player
48860,7     0,03    -0,05   0   Player1
48860,8     0,02    -0,05   0   Player1
48860,9     0,02    -0,04   0   Player1
48861,0     0,02    -0,03   0   Player1
48861,1     0,01    -0,02   0   Player1', fixed = TRUE),header=T)


upsampleDeltaT=seq(from=min(dt$DeltaT),to=max(dt$DeltaT),by=.01)

Speed<-approx(dt$DeltaT,dt$Speed,upsampleDeltaT)$y
Acc<-approx(dt$DeltaT,dt$Acc,upsampleDeltaT)$y
HR<-approx(dt$DeltaT,dt$HR,upsampleDeltaT)$y
Player <- rep(dt$Player,c(rep(10,nrow(dt)-1),1))

data.frame(upsampleDeltaT,Speed,Acc,HR,Player)
#>    upsampleDeltaT Speed    Acc HR  Player
#> 1        48860.70 0.030 -0.050  0 Player1
#> 2        48860.71 0.029 -0.050  0 Player1
#> 3        48860.72 0.028 -0.050  0 Player1
#> 4        48860.73 0.027 -0.050  0 Player1
#> 5        48860.74 0.026 -0.050  0 Player1
#> 6        48860.75 0.025 -0.050  0 Player1
#> 7        48860.76 0.024 -0.050  0 Player1
#> 8        48860.77 0.023 -0.050  0 Player1
#> 9        48860.78 0.022 -0.050  0 Player1
#> 10       48860.79 0.021 -0.050  0 Player1
#> 11       48860.80 0.020 -0.050  0 Player1
#> 12       48860.81 0.020 -0.049  0 Player1
#> 13       48860.82 0.020 -0.048  0 Player1
#> 14       48860.83 0.020 -0.047  0 Player1
#> 15       48860.84 0.020 -0.046  0 Player1
#> 16       48860.85 0.020 -0.045  0 Player1
#> 17       48860.86 0.020 -0.044  0 Player1
#> 18       48860.87 0.020 -0.043  0 Player1
#> 19       48860.88 0.020 -0.042  0 Player1
#> 20       48860.89 0.020 -0.041  0 Player1
#> 21       48860.90 0.020 -0.040  0 Player1
#> 22       48860.91 0.020 -0.039  0 Player1
#> 23       48860.92 0.020 -0.038  0 Player1
#> 24       48860.93 0.020 -0.037  0 Player1
#> 25       48860.94 0.020 -0.036  0 Player1
#> 26       48860.95 0.020 -0.035  0 Player1
#> 27       48860.96 0.020 -0.034  0 Player1
#> 28       48860.97 0.020 -0.033  0 Player1
#> 29       48860.98 0.020 -0.032  0 Player1
#> 30       48860.99 0.020 -0.031  0 Player1
#> 31       48861.00 0.020 -0.030  0 Player1
#> 32       48861.01 0.019 -0.029  0 Player1
#> 33       48861.02 0.018 -0.028  0 Player1
#> 34       48861.03 0.017 -0.027  0 Player1
#> 35       48861.04 0.016 -0.026  0 Player1
#> 36       48861.05 0.015 -0.025  0 Player1
#> 37       48861.06 0.014 -0.024  0 Player1
#> 38       48861.07 0.013 -0.023  0 Player1
#> 39       48861.08 0.012 -0.022  0 Player1
#> 40       48861.09 0.011 -0.021  0 Player1
#> 41       48861.10 0.010 -0.020  0 Player1

CodePudding user response:

library(data.table)
library(zoo)
set.seed(123)
# 10Hz and 100Hz sample data
DT10 <- data.table(time = seq(0,1, by = 0.1), value = sample(1:10, 11, replace = TRUE))
DT100 <- data.table(time = seq(0,1, by = 0.01), value = sample(1:10, 101, replace = TRUE))
# you should use setDT() if your data is not already data.table format
# join the DT10 to DT100
DT100[DT10, value2 := i.value, on = .(time)]
# intyerpolate NA-values
DT100[, value2_inter := zoo::na.approx(value2)]

#output
head(DT100, 31)
#     time value value2 value2_inter
#  1: 0.00     3      3          3.0
#  2: 0.01     9     NA          3.0
#  3: 0.02     9     NA          3.0
#  4: 0.03     9     NA          3.0
#  5: 0.04     3     NA          3.0
#  6: 0.05     8     NA          3.0
#  7: 0.06    10     NA          3.0
#  8: 0.07     7     NA          3.0
#  9: 0.08    10     NA          3.0
# 10: 0.09     9     NA          3.0
# 11: 0.10     3      3          3.0
# 12: 0.11     4     NA          3.7
# 13: 0.12     1     NA          4.4
# 14: 0.13     7     NA          5.1
# 15: 0.14     5     NA          5.8
# 16: 0.15    10     NA          6.5
# 17: 0.16     7     NA          7.2
# 18: 0.17     9     NA          7.9
# 19: 0.18     9     NA          8.6
# 20: 0.19    10     NA          9.3
# 21: 0.20     7     10         10.0
# 22: 0.21     5     NA          9.8
# 23: 0.22     7     NA          9.6
# 24: 0.23     5     NA          9.4
# 25: 0.24     6     NA          9.2
# 26: 0.25     9     NA          9.0
# 27: 0.26     2     NA          8.8
# 28: 0.27     5     NA          8.6
# 29: 0.28     8     NA          8.4
# 30: 0.29     2     NA          8.2
# 31: 0.30     1     NA          8.0
# time value value2 value2_inter

CodePudding user response:

Have a look at the approx function. It interpolates data for new points by.

  • Related