How to filter data from a data frame in R?

0

I have a data frame (out.predict.garch) in, what I want to get is to filter the data following the condition that the variables out.predict.garch $ name is equal to one of the levels (ibex) (that if it is done) and that the variable out.predict.garch $ error is the smallest value, thus staying with a single observation in the new data frame. I have found several formulas for filtering data frame, subset, select, filtrer, but none of them find the lower value of a numeric variable.

    
asked by agustin marin 16.11.2018 в 10:26
source

2 answers

1

Without a reproducible example it is difficult to respond. There are several ways to do it, one option could be using the function which.min() :

out.predict.garch.ibex <- out.predict.garch[out.predict.garch$nombre=='ibex', ]
out.predict.garch.ibex[which.min(out.predict.garch.ibex$error), ]

I hope you solve your problem.

    
answered by 16.11.2018 в 13:47
0

Without a doubt the answer of P. Paccioretti is the appropriate one, but in any case I offer you an approximation to the problem using the package included in tidyverse . Using the pipe %>% plus the data manipulation verbs, once you get used to it, it can be very practical, performant and tremendously clear / readable.

Your problem could be solved using the "verb" filter :

library(tidyverse)

out.predict.garch %>%             # a partir de out.predict.garch  
    filter(nombre == "DIA",       # filtramos por nombre == "DIA"
           error == min(error))   # y por aquellos dónde tenemos el mínimo error
    
answered by 16.11.2018 в 18:49