# help creating subsets using filter from dplyr

Hi there,

I need to create subsets from a series of numeric values. I've created a .csv file with two columns (fragment below). the first column (onsets) has the data from which I want to extract the subsets, and the second column (phrase.constraint) provides the limits of each subset.

onsets phrase.constraint
0.6502 0.65
0.7271 3.512
0.8998 13.3079
1.0681
1.2702
1.6718
1.8736
2.0898
2.4294
2.5426
2.8473
3.5367
3.692
3.7849
5.6874
6.6873
7.2838
7.9644
8.5595
9.1138
9.4723
9.8757
10.1152
10.2777
10.4795
10.6246
10.7624
10.8829
10.954
14.051

My aim is to write a code that will create subgroups considering the range provided by the column "phrase.constraint". The first subset would be composed of the numbers between 0.65 and 3.512 (the two first values from column "phrase.contraint". Therefore, it would include the eleven numbers from 0.6502 till 2.8473.

I tried the following code:

library(dplyr)
phrases=as.data.frame(phrases)

Subset1 = filter(phrases, between(phrase.constraint, 1,2 ))

The goal was that 1 and 2 would indicate the rows from "phrase.contraint" that would limit my subset - in this case, it would select the eleven numbers between 0.65 3.512, but I can't seem to figure out how to do that.

This is my first post so I'm sorry if this is not very clear.

Hi,

To get more specific help from us, please provide a REPREX for it helps copying-pasting code and making improvements.

I do not understand your dataframe, as I would expect equal length columns. As a first step to help you, maybe rewriting the `filter()` statement into

``````filter(phrases, between(onsets, phrase.constraint[1], phrase.constraint[2]))
#    onsets phrase.constraint
# 1  0.6502            0.6500
# 2  0.7271            3.5120
# 3  0.8998           13.3079
# 4  1.0681            0.6500
# 5  1.2702            3.5120
# 6  1.6718           13.3079
# 7  1.8736            0.6500
# 8  2.0898            3.5120
# 9  2.4294           13.3079
# 10 2.5426            0.6500
# 11 2.8473            3.5120

``````

Hope this helps you forward in some way.

JW

Yes, this was exactly what I was looking for. I will provide a Reprex for the next time.

Thank you!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.