Carina
Carina

Reputation: 33

Converting Indicator Columns to Counts Table?

In R, I am trying to convert a data frame of indicator variables into a table of counts.

My data looks like this, where A, B, C, and D are indicator variables for different conditions and ID1, ID2, ID3, and ID4 are patient ID's.

test <- data.frame(rbind(c(0,0,0,1),c(1,0,0,1),c(1,0,0,0),c(1,1,0,0)))
colnames(test) <- c("A","B","C","D")
rownames(test) <- c("ID1","ID2","ID3","ID4"); test

    A B C D
ID1 0 0 0 1
ID2 1 0 0 1
ID3 1 0 0 0
ID4 1 1 0 0

I'd like to end up with a table where it does a summary of the counts for A, B, C, and D and how they overlap:

  A B C D
A 3 1 0 1
B 1 1 0 0
C 0 0 0 0
D 1 0 0 2

Since A, B, C, and D are different variables and a given ID can have multiple entries, I'm not sure how to make this happen. Thanks for your help!

Upvotes: 1

Views: 58

Answers (1)

Consistency
Consistency

Reputation: 2922

Does matrix multiplication solve your problem?

test <- data.frame(rbind(c(0,0,0,1),c(1,0,0,1),c(1,0,0,0),c(1,1,0,0)))
colnames(test) <- c("A","B","C","D")
rownames(test) <- c("ID1","ID2","ID3","ID4"); test

test1 <- as.matrix(test)
t(test1) %*% test1

Upvotes: 5

Related Questions