0

I have a character vector. I want to count the order of appreance of duplicates in this vector, so that each apperance has a different "id":

   crop = c("spring barley", "winter wheat", "potatoe", "spring barley", "alfalfa", "winter wheat", "winter wheat")

As an output i expect something like the behaviour of make_clean_names() from the janitor package, which creates a vector like this:

id = janitor::make_clean_names(crop)
df = data.frame(crop,id)
df
          crop              id
 spring barley   spring_barley
  winter wheat    winter_wheat
       potatoe         potatoe
 spring barley spring_barley_2
       alfalfa         alfalfa
  winter wheat  winter_wheat_2
  winter wheat  winter_wheat_3

but instead I only want the number of apperance in the order of the rows:

id = count_appreances(crop)
df = data.frame(crop,id)
df
             crop id
    spring barley  1
     winter wheat  1
          potatoe  1
    spring barley  2
          alfalfa  1
     winter wheat  2
     winter wheat  3

I know I could either write a for loop, or extract the last character from the jantior function, but I was wondering weather there is a package that already has an elegant function for this.

0 Answers0