User Defined Function returning wrong values when passing a Data Frame as argument

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


User Defined Function returning wrong values when passing a Data Frame as argument



I have a function to make interpolations that goes as below:


interpola <- function(DF, vlBase, interpolY = TRUE) {
DataFrameInterpol <- data.frame(DF)
FunctionVlBase <- as.numeric(vlBase)
InterpolaVar <- interpolY

if (InterpolaVar) n <- 1 else n <- 2

df1 <- filter(data.frame(DataFrameInterpol),
data.frame(DataFrameInterpol[, n]) <= FunctionVlBase) %>% top_n(1)
x1 <- df1[, 1]
y1 <- df1[, 2]

df2 <- filter(data.frame(DataFrameInterpol),
data.frame(DataFrameInterpol[, n]) >= FunctionVlBase) %>% top_n(-1)
x2 <- df2[, 1]
y2 <- df2[, 2]

# Se interpolY == TRUE
# Retorna Y
if (InterpolaVar) {
# Checa se vlBase está na DF
if (x1 == x2) {
return(y1)
}
else {
tmp <- (FunctionVlBase - x1) * (y2 - y1) / (x2 - x1) + y1
return(tmp)
}
}

# Se interpolY <> TRUE
# Retorna X
else if (!InterpolaVar) {
# Checa se vlBase está na DF
if (y1 == y2) {
return(x1)
}
else {
tmp <- (FunctionVlBase - y1) * (x2 - x1) / (y2 - y1) + x1
return(tmp)
}
}
}



The function receives a DataFrame with 2 columns (x,y) that are the base values for the interpolation, a value to be used as the point that i want to interpolate and a boolean argument to define if i the interpolation should made under the x or y access.



The function works fine when the vlBase is a numeric argument but when i try to pass a DataFrame column (e.g. mutate(BaseDate, interpola(InterpolaDF, BaseData$Values))) it gives me wrong results.



Can someone help me ?





can you provide a sample dput(df) output of your data ?
– YOLO
1 hour ago


dput(df)





One of your first lines is DataFrameInterpol <- data.frame(DF). From that point forward, DataFrameInterpol is a data frame, so don't wrap it in data.frame() when you use it. It's already a data frame.
– Gregor
1 hour ago


DataFrameInterpol <- data.frame(DF)


DataFrameInterpol


data.frame()





@YOLO: I have the following DataFrames: InterpolaDF: BusinessDaysToMat RateAcum 0 1.000246 6 1.001476 29 1.007173 48 1.011959 dateVector: Date BusinessDaysToMat 2018-07-24 0 2018-07-25 1 2018-07-26 2 2018-07-27 3 2018-07-28 3 2018-07-29 3 2018-07-30 4 2018-07-31 5 ... If i execute something like interpola(InterpolaDF, 47) i get 1.011707 (wich is the expected return), but if do interpola(InterpolaDF, dateVector$BusinessDaysToMat) on the row that has BusinessDaysToMat = 47 i have the return of 1.000246.
– Andre Vk Freitas
13 mins ago





@Gregor Thanks for the tip.
– Andre Vk Freitas
12 mins ago





Please include your sample data in the question, don't bury it in a comment.
– Gregor
5 mins ago









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

C++ virtual function: Base class function is called instead of derived