Home > database >  Counting values in multiple columns
Counting values in multiple columns

Time:10-22

everyone. Here's the problem:

Column1  Column2
da          fa
fa          da
ra          da
ta          ta

I need to count how many times each string appears in both columns: I mean, "da" appears 1 time in Column1 and 2 times in Column2, so I need the count of 3. Ps.: I can't stack them, because it will generate new rows with null values in my dataframe.

CodePudding user response:

We can use sum on a logical matrix to return the count as TRUE -> 1 and FALSE -> 0 in base R

sum(df1 == "da", na.rm = TRUE)
[1] 3

If we need it by columns, use colSums

colSums(df1 == "da", na.rm = TRUE)
Column1 Column2 
      1       2 

If we want all the frequency counts in base R

table(stack(df1)$values)

da fa ra ta 
 3  2  1  2 

data

df1 <- structure(list(Column1 = c("da", "fa", "ra", "ta"), Column2 = c("fa", 
"da", "da", "ta")), class = "data.frame", row.names = c(NA, -4L
))

CodePudding user response:

Using pivot_longer

library(dplyr)
library(tidyr)

df1 %>% pivot_longer(cols = everything()) %>% count(value)
# A tibble: 4 × 2
  value     n
  <chr> <int>
1 da        3
2 fa        2
3 ra        1
4 ta        2
  •  Tags:  
  • r
  • Related