Home > Software design >  how can I get a list of number of repeats of a given data set in r?
how can I get a list of number of repeats of a given data set in r?

Time:08-16

I have an ID recording column containing the characters:

39299
30299
39299
39299
38744
38744
27222
39299
29000
38744
29000
29000
29000.

How can I code to make a new column that shows the number of repeats? 39299 repeated 4 times, 30299 repeated 1 time, 38744 repeated 3 times, 27222 repeated 1 time, and 29000 repeated 4 times, then the output looks like c(4, 1, 3, 4)?

CodePudding user response:

You can do as.vector(table(your_vector)) to get the number of appearances of each number in a vector. Check table only as well.

vec = c(2, 2, 3, 1, 4)
as.vector(table(factor(vec, levels = unique(vec))))
[1] 2 1 1 1

CodePudding user response:

If this is a column in a data frame, you can use dplyr solution.

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

# test data frame
df = data.frame(vec = c(2, 2, 3, 1, 4))

# add column with counts
df %>% add_count(vec)
#>   vec n
#> 1   2 2
#> 2   2 2
#> 3   3 1
#> 4   1 1
#> 5   4 1

# count unique values in column
df %>% count(vec)
#>   vec n
#> 1   1 1
#> 2   2 2
#> 3   3 1
#> 4   4 1

# count unique values and order as in the original data
df %>% 
  mutate(vec = factor(vec, levels = unique(vec)))%>% 
  count(vec)
#>   vec n
#> 1   2 2
#> 2   3 1
#> 3   1 1
#> 4   4 1

Created on 2022-08-15 by the reprex package (v2.0.0)

  •  Tags:  
  • r
  • Related