Search code examples
rdataframesubsetintervals

Subsetting a data frame with R, by intervals of the data


I am trying to subset with R a big data frame (df). It has many columns and one of them is called Ratio_qubit_TS. I would like to subset it in a smaller dataframe (df_0.05) that only contains the values of Ratio_qubit_TS that are >= 0.5 and <1, with of course the other columns of my data frame that correspond to the selected rows. Then, I'll repeat this subsetting for different intervals of Ratio_qubit_TS.

I have tried to create a first subset of my data (=df_0.5), using this code, but I don't understand why it doesn't work : df_0.5 <- df[df$Ratio_qubit_TS >=0.5 & df$Ratio_qubit_TS <= 1]

The error message says : Error in [.data.frame(df, df$Ratio_qubit_TS >= "0.5" & df$Ratio_qubit_TS <= : undefined columns selected

If you had any advice on how I could solve that problem, it would be wonderful. It has been hours that I'm trying to find a solution and could not find any. And I you had an idea on how to make R repeat the process of subsetting my data frame for different intervals of Ratio_qubit_TS (ex : 1<=Ratio_qubit_TS<1.5, then 1.5<=Ratio_qubit_TS<2...etc), I would be so so grateful.

Thanks :)

PS : It's the first time that I post on this forum and besides English is not my mothertongue, so I hope that you will understand easily my problem.


Solution

  • Try this:

    library(dplyr)
    
    df_0.05 <- df %>% filter(between(Ratio_qubit_TS, 0.5, 1))