Counting the number of elements with the values of x in a vector

后端 未结 19 1349
闹比i
闹比i 2020-11-22 02:44

I have a vector of numbers:

numbers <- c(4,23,4,23,5,43,54,56,657,67,67,435,
         453,435,324,34,456,56,567,65,34,435)

How can I hav

19条回答
  •  悲&欢浪女
    2020-11-22 03:26

    This is a very fast solution for one-dimensional atomic vectors. It relies on match(), so it is compatible with NA:

    x <- c("a", NA, "a", "c", "a", "b", NA, "c")
    
    fn <- function(x) {
      u <- unique.default(x)
      out <- list(x = u, freq = .Internal(tabulate(match(x, u), length(u))))
      class(out) <- "data.frame"
      attr(out, "row.names") <- seq_along(u)
      out
    }
    
    fn(x)
    
    #>      x freq
    #> 1    a    3
    #> 2     2
    #> 3    c    2
    #> 4    b    1
    

    You could also tweak the algorithm so that it doesn't run unique().

    fn2 <- function(x) {
      y <- match(x, x)
      out <- list(x = x, freq = .Internal(tabulate(y, length(x)))[y])
      class(out) <- "data.frame"
      attr(out, "row.names") <- seq_along(x)
      out
    }
    
    fn2(x)
    
    #>      x freq
    #> 1    a    3
    #> 2     2
    #> 3    a    3
    #> 4    c    2
    #> 5    a    3
    #> 6    b    1
    #> 7     2
    #> 8    c    2
    

    In cases where that output is desirable, you probably don't even need it to re-return the original vector, and the second column is probably all you need. You can get that in one line with the pipe:

    match(x, x) %>% `[`(tabulate(.), .)
    
    #> [1] 3 2 3 2 3 1 2 2
    

提交回复
热议问题