Home > Enterprise >  Select first occurrence of a decimal value in R
Select first occurrence of a decimal value in R

Time:07-29

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

CodePudding user response:

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

CodePudding user response:

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
  • Related