在 R 中有效地重采样数据(线性外推)
Resampling data efficiently in R (linear extrapolation)
我有一组以 1 秒为间隔记录的观察结果(伽玛和时间)。我想在 0.1 秒时重新采样这些数据。数据如下所示:
38804.96 12.59222222
38805.12 12.5925
38805.38 12.59277778
38805.4 12.59305556
38805.27 12.59333333
38805.36 12.59361111
38805.33 12.59388889
38805.23 12.59416667
38805.3 12.59444444
38805.18 12.59472222
38805.21 12.595
38805.28 12.59527778
我想出了以下代码来重新采样(线性外推)伽玛,但它非常耗时,因为我的数据集有超过 30000 个观测值。
#Resampling la diurnal drift
j <- (0:9)
A <- 0
VectorT <- numeric()
VectorG <- numeric()
for (I in 1:nrow(R20140811)){
# Calculate the increment of time
Rate <- (R20140811[I+1,2]- R20140811[I,2])/10
Time <- R20140811[I,2]
# Calculate the increment of gamma
nT <- (R20140811[I+1,1] - R20140811[I,1])/10
Gamma <- R20140811[I,1]
print(I)
for (j in 0:9){
A <- A + 1
VectorT[A] <- Time + (j*Rate)
VectorG[A] <- Gamma + (j*nT)
R20140811[A,3] <- VectorG[A]
R20140811[A,4] <- VectorT[A]
}
}
你知道更有效的方法吗?
您需要矢量化您的计算。
鉴于您的矩阵:
R20140811 <- matrix(
c(38804.96 ,12.59222222,
38805.12 ,12.5925 ,
38805.38 ,12.59277778,
38805.4 ,12.59305556,
38805.27 ,12.59333333,
38805.36 ,12.59361111,
38805.33 ,12.59388889,
38805.23 ,12.59416667,
38805.3 ,12.59444444,
38805.18 ,12.59472222,
38805.21 ,12.595 ,
38805.28 ,12.59527778),ncol=2,byrow=TRUE)
单独的时间和伽玛:
times <- R20140811[,2]
gammas <- R20140811[,1]
然后定义外推函数:
# given a vector vect, extrapole nInt points between points
Extrapol <- function(vect,nInt){
# the starting points of your intervals
zeros <- vect[1:(length (vect)-1)]
# determine the increments
increments <- (vect[2:length (vect)]-zeros)/nInt
# get the new sample
newSample <- rep(zeros[1: (length (times)-1)],each=10) + as.vector(outer (0:9,increments))
return(newSample)
}
并将外推函数应用于您的时间和 gammas
newSampleGamma <- Extrapol(gammas,10)
newSampleTimes <- Extrapol(times,10)
应该快几个数量级:-)
我有一组以 1 秒为间隔记录的观察结果(伽玛和时间)。我想在 0.1 秒时重新采样这些数据。数据如下所示:
38804.96 12.59222222
38805.12 12.5925
38805.38 12.59277778
38805.4 12.59305556
38805.27 12.59333333
38805.36 12.59361111
38805.33 12.59388889
38805.23 12.59416667
38805.3 12.59444444
38805.18 12.59472222
38805.21 12.595
38805.28 12.59527778
我想出了以下代码来重新采样(线性外推)伽玛,但它非常耗时,因为我的数据集有超过 30000 个观测值。
#Resampling la diurnal drift
j <- (0:9)
A <- 0
VectorT <- numeric()
VectorG <- numeric()
for (I in 1:nrow(R20140811)){
# Calculate the increment of time
Rate <- (R20140811[I+1,2]- R20140811[I,2])/10
Time <- R20140811[I,2]
# Calculate the increment of gamma
nT <- (R20140811[I+1,1] - R20140811[I,1])/10
Gamma <- R20140811[I,1]
print(I)
for (j in 0:9){
A <- A + 1
VectorT[A] <- Time + (j*Rate)
VectorG[A] <- Gamma + (j*nT)
R20140811[A,3] <- VectorG[A]
R20140811[A,4] <- VectorT[A]
}
}
你知道更有效的方法吗?
您需要矢量化您的计算。
鉴于您的矩阵:
R20140811 <- matrix(
c(38804.96 ,12.59222222,
38805.12 ,12.5925 ,
38805.38 ,12.59277778,
38805.4 ,12.59305556,
38805.27 ,12.59333333,
38805.36 ,12.59361111,
38805.33 ,12.59388889,
38805.23 ,12.59416667,
38805.3 ,12.59444444,
38805.18 ,12.59472222,
38805.21 ,12.595 ,
38805.28 ,12.59527778),ncol=2,byrow=TRUE)
单独的时间和伽玛:
times <- R20140811[,2]
gammas <- R20140811[,1]
然后定义外推函数:
# given a vector vect, extrapole nInt points between points
Extrapol <- function(vect,nInt){
# the starting points of your intervals
zeros <- vect[1:(length (vect)-1)]
# determine the increments
increments <- (vect[2:length (vect)]-zeros)/nInt
# get the new sample
newSample <- rep(zeros[1: (length (times)-1)],each=10) + as.vector(outer (0:9,increments))
return(newSample)
}
并将外推函数应用于您的时间和 gammas
newSampleGamma <- Extrapol(gammas,10)
newSampleTimes <- Extrapol(times,10)
应该快几个数量级:-)