dplyr rank returning constant value

The dplyr ranking functions are returning a constant value.
This example uses "row)number()", but the same behavior is encountered with the other ranking functions.
Any idea how to resolve the issue?

Here is the code:

f.rank <- f.sumdata %>% filter(STATE != 0) %>%
mutate(RANK2019_LT18 = row_number(SUM2019_LT18))

which returns:

STATE NAME SUM2019_LT18 RANK2019_LT18
1 Alabama 1088286 1
2 Alaska 179983 1
4 Arizona 1640226 1
5 Arkansas 700155 1
6 California 8894449 1
8 Colorado 1259516 1
9 Connecticut 727440 1
10 Delaware 203572 1

TIA
AB

do you have any groups set on f.sumdata ?
could you share a small portion of your data perhaps by

dput(head(f.sumdata,n=20))

The data in f.sumdata does not have any groups. The data is state-level data and the raw data are in the example (without the last column).

works fine for me... so I speculate there is something with your data that would not be captured by your text representation to this forum.

intext ="STATE NAME SUM2019_LT18 RANK2019_LT18
1 Alabama 1088286 1
2 Alaska 179983 1
4 Arizona 1640226 1
5 Arkansas 700155 1
6 California 8894449 1
8 Colorado 1259516 1
9 Connecticut 727440 1
10 Delaware 203572 1"

example_df <- function(intext) {
tf <- tempfile()
writeLines(intext, con = tf)
require(tidyverse)
as_tibble(read.delim(tf,sep = " "))
}
(df <- example_df(intext) %>% select(-RANK2019_LT18))

df%>% mutate(rank_a = row_number(SUM2019_LT18))
# # A tibble: 8 x 4
# STATE NAME        SUM2019_LT18 rank_a
# <int> <fct>              <int>  <int>
# 1     1 Alabama          1088286      5
# 2     2 Alaska            179983      1
# 3     4 Arizona          1640226      7
# 4     5 Arkansas          700155      3
# 5     6 California       8894449      8
# 6     8 Colorado         1259516      6
# 7     9 Connecticut       727440      4
# 8    10 Delaware          203572      2

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