R Column for cumulative frequency of a row's particular item -
i'm trying create column counts frequency of particular row's value point. code each matrix shows current data , desired results. 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 output frequency column corresponds 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 came before there cumulative count in row. appreciated.
you can use dplyr group data type , return row number each line. because data grouped, row number equal number of times given value of type has appeared.
library(tidyverse) test %>% tbl_df() %>% group_by(type) %>% mutate(frequency = row_number())
Comments
Post a Comment