Dipayan Ganguly
Dipayan Ganguly

Reputation: 1

Loop to create bivariate/cross table

I am trying to create a loop where I want get the frequency between column 1 and column 2,column 1 and column 3....till col1 and col30.

Col1 col2 col3
0        A     25
1        A     30
0        A     30
1        B     20
0        B     20

Output.

     0    1               0      1
 A   2    1         25    0      0
 B   1    1         30    1      1
                    20    1      1

Upvotes: 0

Views: 100

Answers (2)

akrun
akrun

Reputation: 887088

We can use tidyverse

library(tidyverse)
map(names(df)[-1], ~  cbind(df[1], df[.x]) %>% 
           count(Col1, !! rlang::sym(.x)) %>%
           spread(Col1, n, fill = 0))

data

df <- structure(list(Col1 = c(0L, 1L, 0L, 1L, 0L), col2 = structure(c(1L, 
1L, 1L, 2L, 2L), .Label = c("A", "B"), class = "factor"), col3 = c(25L, 
30L, 30L, 20L, 20L)), class = "data.frame", row.names = c(NA, -5L))

Upvotes: 1

Ronak Shah
Ronak Shah

Reputation: 388982

Use lapply to loop over columns and then table to calculate frequency

lapply(df[-1], function(x) table(x, df[, 1]))

#$col2

#x   0 1
#  A 2 1
#  B 1 1

#$col3

#x    0 1
#  20 1 1
#  25 1 0
#  30 1 1

Or a shorter version using Map

Map(table, df[1], df[-1])

data

df <- structure(list(Col1 = c(0L, 1L, 0L, 1L, 0L), col2 = structure(c(1L, 
1L, 1L, 2L, 2L), .Label = c("A", "B"), class = "factor"), col3 = c(25L, 
30L, 30L, 20L, 20L)), class = "data.frame", row.names = c(NA, -5L))

Upvotes: 1

Related Questions