0

Ok, I have been trying to get an answer for this but I cant find it anywhere, but it seems like an easy task (which is bugging me even more!)

I have a dataframe with a series of numbers in a column which I want to filter to get the first occurrence of a number....for example, if i have 1.01, 1.08, 1.15, I want to filter the rows to get the row with the value 1.01 in that column.

An examples is:

x<- c(2.04, 2.25, 3.99, 3.20, 2.60, 1.85, 3.57, 3.37, 2.59, 1.60, 3.93, 1.33, 1.08, 4.64, 2.09, 4.53, 3.04, 3.85, 3.15, 3.97)
y<- c(2.62, 2.48, 1.40, 2.27, 3.71, 1.86, 3.56, 2.08, 2.36, 3.23, 1.65, 3.43, 1.57, 4.49, 2.29, 3.32, 2.12, 4.45, 1.57, 4.70)
z <- data.frame(x, y)
z <- z[order(z$x, decreasing = FALSE), ]

And the filtered results should be:

x    y
1.08 1.57
2.04 2.62
3.04 2.12
4.53 3.32

Any help would be apprreciated

  • Decimal values will not consistently match. [Why are these numbers not equal?](https://stackoverflow.com/questions/9508518/why-are-these-numbers-not-equal). Also FAQ [7.31 Why doesn’t R think these numbers are equal?](https://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f). If you really have to do this, you can convert the values to two decimal characters and match the character strings, e.g. "1.01" == "1.01". – dcarlson Jul 29 '22 at 02:19
  • But that is just the example.....what i want to filter is the first occurrence of 1, the first occurrence of 2 etc (when sorted)..It doesnt matter whether decimal value not consistently matching...that isnt the issue...if there was an value of 1.01 in the x column then that row would be included – Stats_Dunce Jul 29 '22 at 02:34

2 Answers2

1
z %>%
  arrange(x) %>%
  group_by(int = floor(x)) %>%
  slice(1) %>%
  ungroup()

# A tibble: 4 × 3
      x     y   int
  <dbl> <dbl> <dbl>
1  1.08  1.57     1
2  2.04  2.62     2
3  3.04  2.12     3
4  4.53  3.32     4

or

z %>%
  arrange(x) %>%
  filter(floor(x) != lag(floor(x), default = 0))

     x    y
1 1.08 1.57
2 2.04 2.62
3 3.04 2.12
4 4.53 3.32
Jon Spring
  • 55,165
  • 4
  • 35
  • 53
0

You can also try this:

z1 <- z %>%
     group_by(floor(z$x)) %>%
     arrange(z$x) %>%
     filter(row_number()==1)
 z1
# A tibble: 4 × 3
# Groups:   floor(z$x) [4]
      x     y `floor(z$x)`
  <dbl> <dbl>        <dbl>
1  1.08  1.57            1
2  2.04  2.62            2
3  3.04  2.12            3
4  4.53  3.32            4
XixiHaha
  • 138
  • 5