'How to count number of occurrences of specific value in data frame
I need to find a very efficient way to calculate the number of times each of 3 specific values occurs in a data frame. Here is what my data frame looks like:
The only values that can be found there are "0/1", "1/1", and "0/0". I want the output to be in a form of 3 different variables containing the respective number of occurrences.
Edit:
As mentioned in the comments, I tried to use table(unlist(DF)), but I reckon my data frame is too big.
Solution 1:[1]
As suggested by RobZ in the comments, table(unlist(df, use.names = F)) is quite speedy, but using a matrix or vector-like structure is faster. Seeing this SO post, and inspecting table, it calls tabulate under the hood. We can dig into it a little if we are sure no edge cases exist.
bench::mark(
one = table(unlist(df, use.names = F)),
two = .Internal(tabulate(
unlist(df, use.names = F ) |> # make a vector
(\(.) ifelse(. == "0/0", 1L, ifelse(. == "0/1", 2L, 3L)))(), # converting to integer
3)), check = F
)
# A tibble: 2 x 13
expression min median `itr/sec` mem_alloc `gc/sec` n_itr n_gc total_time
<bch:expr> <bch:tm> <bch:t> <dbl> <bch:byt> <dbl> <int> <dbl> <bch:tm>
1 one 62.8us 67us 13689. 0B 6.26 6555 3 479ms
2 two 11.1us 12.3us 76757. 0B 7.68 9999 1 130ms
Since we defined the "factor levels" ourselves, we can infer the meaning of the numbers. Note that the output is not the same, it is the cost of speed versus convenience. Another thing to consider is premature optimization: the code in this post is just for educational purposes, base R functions are usually quite optimized for the general use case, minor tweaks probbaly take longer to implement than they save on top of the risk of errors if edge cases are actually present.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | akrun |


