View source: R/data_interpolation.R
| bgf_interpolation | R Documentation |
A function that allows to interpolate missing values in a numeric vector of a data.frame.
It requires a second column in the data.frame representing time, as well as a third column representing grouping information.
bgf_interpolation(df, x, t, group, end = TRUE, sub_zero = NULL, offset = 1)
df |
a |
x |
an |
t |
an |
group |
an |
end |
|
sub_zero |
default is |
offset |
an |
The function bgf_interpolation takes a data.frame as input and returns a modified version of it as output.
In particular, it interpolates missing values in a numeric vector (argument 'x') based on a second vector (argument 't') without missing values.
It is possible to stop the interpolation (argument 'end'), if no valid value in 'x' occurs after a missing value, or to use linear extrapolation to fill terminal NA's.
The slope of the extrapolation is based on the last valid 'x' value and the n^{th} valid 'x' value before (argument 'offset').
Furthermore, if linear extrapolation leads to e.g. negative gas concentrations or volumes, it is possible to replace these wrong values with any replacement (argument 'sub_zero')
Interpolation and extrapolation are done specifically for groups within the data (argument 'group')
a data.frame
# create an example data.frame
gap_data=data.frame(
time=as.numeric(rep(c(1:30),3)),
value=c(0,rep(NA,3),7,14,26,44,72,rep(NA,8),314,350,
377,rep(NA,5),471,rep(NA,4),0,rep(NA,7),100,
127,163,rep(NA,4),217,rep(NA,5),267,rep(NA,4),
0,rep(NA,3),0,rep(NA,3),5,rep(NA,3),12,rep(NA,3),
32,rep(NA,3),35,rep(NA,3),33,rep(NA,3),28,rep(NA,3),
5,NA),
cat=c(rep("exGas",30),rep("exGas_2",30),rep("cBioG",30)))
# interpolate and extrapolate missing values, replace values below '0' with '0'
closed_data<- bgf_interpolation(gap_data,2,1,3,end = FALSE,sub_zero = 0)
# inspect the differences
plot(value ~ time,data=gap_data,col=as.factor(cat))
legend(legend = levels(as.factor(gap_data$cat)),col=c(1,2,3),x = "topleft",pch=15)
plot(value ~ time,data=closed_data,col=as.factor(cat))
legend(legend = levels(as.factor(closed_data$cat)),col=c(1,2,3),x = "topleft",pch=15)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.