Posts

Showing posts with the label user-defined-functions

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

Image
Clash 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 ...