Search code examples
rfrequency

R Column for cumulative frequency of a row's particular item


I'm trying to create a column that counts the frequency of that particular row's value up to that point. The code for each matrix shows the current data and the desired results. So for a matrix of columns Type | Value:

test <- matrix(c(
1,0.14,
1,0.1345,
2,1.245,
2,1.532,
3,3.5345,
3,2.987,
2,1.743),ncol=2, byrow=TRUE)

colnames(test) <- c("Type", "Value")

I'm trying to get an output frequency column that corresponds to the type column:

test <- matrix(c(
1,0.14,1,
1,0.1345,2,
2,1.245,1,
2,1.532,2,
3,3.5345,1,
3,2.987,2,
2,1.743,3),ncol=3, byrow=TRUE)

colnames(test) <- c("Type", "Value","Frequency")

For each sum of Type that came before there is a cumulative count in that row. Any help would be appreciated.


Solution

  • You can use dplyr to group the data by Type and then return the row number for each line. Because the data are grouped, the row number will equal the number of times the given value of Type has appeared.

    library(tidyverse)
    
    test %>% 
      tbl_df() %>% 
      group_by(Type) %>% 
      mutate(Frequency = row_number())