function similar to COUNTIF in R

I have data of NHPM_IDs repeating in dataframe as in sample image as follow

need to create count NHPM id column next to NHPM_id as follows

This task is to find out how many times each NHPM_ID is appearing in column( for ex an NHPMID "ABC" is appearing 3 times in row no 12, 500, 1221 consecutively, I need column saying that

  1. ABC in row no 12 appears 3 times in column, when counted rows from 12 to last Row of data
  2. ABC in row no 500 appears 2 times in column, when counted rows from 500 to last Row of data
  3. ABC in row no 1221 appears 1 time in column, when counted rows from 1221 to last Row of data

this task is to ease the PIVOT table for count of unique NHPM_ids with other variables
I have 5 to 6 lacs rows need to perform this task every day, help will be appreciated

here are the raw ingredients for the type of thing you are asking about doing.

library(tidyverse)

(example_df <- tibble(c1=c("a a",letters[1:4]),
                     c2=c(letters[4:1],"a a a")))

(interim_df <- mutate(example_df,
       across(c(c1,c2),
              ~str_count(.x,"a"))) )

(summary_df <- summarise(interim_df,
                      across(c(c1,c2),
                            sum)))

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