creating sequential numbers from a single total count

creating numbers

Hello,

I have problems working out the following function. I made a small example in the attached excel sheet.
We have a specific software package that produces reports with only a total amount of counts per department, given in the last record per department (by simple example in the thirst two columns).

What I need R to do, is give me a third vector with sequential numbering as in the example.

Thanks

this ?

library(tidyverse)

ex1 <- tibble(dep=1:3,
              amount=c(10,5,6))

(ex2 <- uncount(ex1,weights = amount) |> 
  group_by(dep) |> 
  mutate(out=row_number()))

Dear nirgrahamuk,

My apologies for my late reply. Yes your solution was OK, but worked only for my example. The real dataframe is much larger then the example so the 'amount' needs to be detected by R automatically instead of being filled in by the user.

I finally came up with this, which works for my entire data-set. (names of the vectors are different from the example. Thanks for thinking with me.

VW2022AR$Aantal_sequential <- ave(VW2022AR$PedigreeItem, VW2022AR$PedigreeItem, FUN=seq_along)

This topic was automatically closed 42 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.