I am trying to find a more efficient way to incrementally count unique data points in a data frame.
For example, I have the following code:
df = matrix(c(1,2,3,3,4,5,1,2,4,4)) count = matrix(nrow = nrow(df),ncol=1) for (i in 1:nrow(df)) { count[i,1] = length(which(df[1:i,1] == df[i,1])) }
The purpose of the code is to gradually count each instance of a specific value, for example. the count column will have the following result:
1,1,1,2,1,1,2,2,2,3.
The code that I have written so far does the job, however the df example above contains only 10 values. The real data frame that I am trying to execute with this function contains 52,118 values , which takes a huge amount of time.
Does anyone know a more efficient way to execute the code above?
r count dataframe
Chintan desai
source share