Bcohen
Bcohen

Reputation: 35

How can I count times a pattern of variables occurs?

I'm trying to count the number of times a pattern of values occurs for variables in R:

My dataset is similar to this:

   a b c d   n
1  0 1 0 0 236
2  0 1 0 1  40
3  0 1 1 0  11
4  0 1 1 1   6
5  0 0 0 0 352
10 0 0 0 1  38

I created this data through using this code to count the number of times each pattern occurs in the dataframe:

df %>% 
  count(a,b,c,d)

I'm trying to count for example how many times the variable A has the value of 1 at the same time as other variables,

for example: d is answered with a value of 1 for once instance in other columns for a total of: 1 instances ( which gives an sum from the n column of 38) d is answered with a value of 1 for two instances in other columns for a total of: 2 instances ( which gives an sum from the n column of 51) d is answered with a value of 1 for three instances in other columns for a total of: 1 instances ( which gives an sum from the n column of 6)

Is there a way in R to calculate this for each of the variables a-d, perhaps with ddply?

the code for the dataframe is:

df <- structure(list(a = structure(c(2L, 2L, 2L, 2L, 2L, 2L), .Label = c("-98", 
"0", "1"), class = "factor"), b = structure(c(2L, 2L, 2L, 2L, 
1L, 1L), .Label = c("0", "1"), class = "factor"), c = structure(c(1L, 
1L, 2L, 2L, 1L, 1L), .Label = c("0", "1"), class = "factor"), 
    d = structure(c(2L, 3L, 2L, 3L, 2L, 3L), .Label = c("-98", 
    "0", "1"), class = "factor"), n = c(236L, 40L, 11L, 6L, 352L, 
    38L)), row.names = 5:10, class = "data.frame")

My expected output looks like this:

  1     2  3 
A 0     0  0 
B 236   51 6
C 0     11 6
D 38    51 6

Upvotes: 0

Views: 117

Answers (1)

Onyambu
Onyambu

Reputation: 79208

df1 %>%
  type.convert(as.is = TRUE)%>%
  mutate(rowSums(across(a:d)) * across(a:d)) %>%
  pivot_longer(-n) %>%
  filter(value > 0)%>%
  xtabs(n~., .)

   value
name   1   2   3
   b 236  51   6
   c   0  11   6
   d  38  40   6

Upvotes: 1

Related Questions