sequencing for admission of pressure ulcer

Dear there
I am trying to achieve admission sequencing for pressure ulcer.
First admit as 1 and subsequent readmit are as 2, 3 and so on based on the encounter_id which is system generated first come first served basis.
My aim is to achieve the last column called AdmitSequence.
Please advise how I can do it with dplyr or tidyverse packages.
pu.pdf (29.4 KB)
Thank you very much.
G

To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

1 Like
bedsore <- tibble::tribble(
             ~Encounter_Id, ~EncounterNumber, ~PatientNumber, ~LengthOfStay, ~WeightedSeparation, ~wies_revenue, ~Age, ~ICUHours, ~TotalAmount, ~admit.seq,
                 28606573L,     "IP03754053",       "003086",        11.249,               3.543,     18168.504,  79L,    "NULL",  20722.17548,         1L,
                 28610290L,     "IP03758275",       "003086",        15.025,                   0,             0,  79L,    "NULL",  19272.97958,         2L,
                 28617152L,     "IP03766215",       "003086",        14.112,               3.543,     18168.504,  79L,    "NULL",  21257.98986,         3L,
                 28557338L,     "IP03821086",       "005294",         12.11,               1.323,      6784.344,  63L,    "NULL",  17331.50129,         1L,
                 28648516L,     "IP03788083",       "018146",        81.079,                   0,             0,  62L,    "NULL",  111954.6181,         1L,
                 28594562L,     "IP03740394",       "019180",        46.364,               8.907,     45675.096,  84L,    "NULL",  84980.83413,         1L,
                 28593211L,     "IP03841785",       "022976",         3.229,                1.88,       9640.64,  79L,    "NULL",  4828.433024,         1L,
                 28612499L,     "IP03760782",       "027948",         2.257,               1.175,        6025.4,  89L,    "NULL",  4475.560016,         1L,
                 28546281L,     "IP03819368",       "029692",        22.834,               4.781,     24516.968,  86L,    "NULL",  57857.20633,         1L,
                 28592631L,     "IP03827625",       "029692",        41.838,                   0,             0,  87L,    "NULL",  48021.77818,         2L,
                 28625748L,     "IP03776146",       "035543",        13.583,              18.128,     92960.384,  64L,     "326",  139755.1603,         1L,
                 28595503L,     "IP03741452",       "039561",        16.295,                5.14,      26357.92,  87L,    "NULL",   35739.9459,         1L,
                 28592624L,     "IP03813021",       "040674",        58.965,                   0,             0,  96L,    "NULL",  54411.58445,         1L,
                 28581544L,     "IP03839725",       "041164",         22.82,              25.572,    131133.216,  78L,     "384",  248254.2244,         1L,
                 28650158L,     "IP03789918",       "045661",         15.17,               6.027,     30906.456,  86L,    "NULL",  46919.16747,         1L,
                 28592535L,     "IP03795333",       "045661",        13.847,                   0,             0,  86L,    "NULL",  15928.91743,         2L,
                 28605352L,     "IP03752676",       "047273",        12.068,               4.832,     24778.496,  55L,    "NULL",  29475.44939,         1L,
                 28623597L,     "IP03773680",       "056814",         2.133,               0.809,      4148.552,  76L,    "NULL",   2838.52689,         1L,
                 28600876L,     "IP03747569",       "056953",        42.799,              10.863,     55705.464,  73L,     "270",  150516.5785,         1L,
                 28615281L,     "IP03764010",       "056953",        26.965,                   0,             0,  73L,    "NULL",  33786.31415,         2L,
                 28585413L,     "IP03793110",       "057001",         8.544,               2.392,     12266.176,  91L,    "NULL",   12617.7798,         1L,
                 28554215L,     "IP03819305",       "057001",        53.247,               9.215,      47254.52,  91L,    "NULL",  88505.84141,         2L
             )
head(bedsore)
#> # A tibble: 6 × 10
#>   Encoun…¹ Encou…² Patie…³ Lengt…⁴ Weigh…⁵ wies_…⁶   Age ICUHo…⁷ Total…⁸ admit…⁹
#>      <int> <chr>   <chr>     <dbl>   <dbl>   <dbl> <int> <chr>     <dbl>   <int>
#> 1 28606573 IP0375… 003086     11.2    3.54  18169.    79 NULL     20722.       1
#> 2 28610290 IP0375… 003086     15.0    0         0     79 NULL     19273.       2
#> 3 28617152 IP0376… 003086     14.1    3.54  18169.    79 NULL     21258.       3
#> 4 28557338 IP0382… 005294     12.1    1.32   6784.    63 NULL     17332.       1
#> 5 28648516 IP0378… 018146     81.1    0         0     62 NULL    111955.       1
#> 6 28594562 IP0374… 019180     46.4    8.91  45675.    84 NULL     84981.       1
#> # … with abbreviated variable names ¹​Encounter_Id, ²​EncounterNumber,
#> #   ³​PatientNumber, ⁴​LengthOfStay, ⁵​WeightedSeparation, ⁶​wies_revenue,
#> #   ⁷​ICUHours, ⁸​TotalAmount, ⁹​admit.seq

Thanks @andresrcs
I repex with datapasta just now.
I want to achieve admit.seq which is the last column assigning index admission as 1 and subsequent admissions from 2 onwards.
These bedsore or pressure ulcer patients readmitted frequently.
Hope you could help me in this.
At the moment, I assign manually for admit.seq.
G

bedsore |>
  arrange(PatientNumber, Encounter_Id) |>
  group_by(PatientNumber) |>
  mutate(admit.seq.calced = row_number())
1 Like

@nirgrahamuk Perfect. It works. Thank you very much. Stunned by your latest R pipe. G

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.