How to find the frequency of continuously repeated string values in an R data frame column?

R ProgrammingServer Side ProgrammingProgramming

To find the frequency of continuously repeated string values in an R data frame column, we can follow the below steps −

  • First of all, create a data frame with a string column.
  • Then, use sequence function and rleid function after converting the data frame into data.table object to find the frequency of continuously repeated string values in data frame column.

Create the data frame

Let's create a data frame as shown below −

 Live Demo

x<-sample(c("f","m"),20,replace=TRUE)
df<-data.frame(x)
df

On executing, the above script generates the below output(this output will vary on your system due to randomization) −

    x
1 m
2 m
3 f
4 m
5 f
6 m
7 m
8 m
9 m
10 f
11 m
12 m
13 f
14 m
15 m
16 m
17 f
18 m
19 m
20 m

Find the frequency of continuously repeated string values

Loading data.table package and setting df to data.table object then finding the frequency of continuously repeated string values −

x<-sample(c("f","m"),20,replace=TRUE)
df<-data.frame(x)
library(data.table)
setDT(df)[,Freq:=sequence(.N),by=rleid(x)][x==1,Freq:=1][]

Output

    x Freq
1:  m 1
2:  m 2
3:  f 1
4:  m 1
5:  f 1
6:  m 1
7:  m 2
8:  m 3
9:  m 4
10: f 1
11: m 1
12: m 2
13: f 1
14: m 1
15: m 2
16: m 3
17: f 1
18: m 1
19: m 2
20: m 3
raja
Published on 04-Aug-2021 11:11:21
Advertisements