EskCargo
EskCargo

Reputation: 35

R: Count or contingency tables produced using a loop

I am new to programming in R so I looking for help to start me off. I have a "long" dataset with 4 columns (which I reshaped earlier in R):

d=matrix(c(1,2,3,4,5,6,7,"A","A","A","B","B","B","B",0,0,99,0,0,99,0,99,0,99,99,0,0,99), ncol=4)
colnames(d) = c("ID","S.ID","ValA","ValB"); dt=as.table(d)

I'd like to create, for each S.ID separately (ie A and B), a contingency table for counts of ValA and ValB

For example (here) --

enter image description here

In my actual example I have 17 S.IDs and I'd like to write a program that will generate the contingency tables for each of these.

Additionally, Will this be easier to do in R Studio? If so, please can you also detail how?

Thank you very much!

-Thanks to all who answered so prompltly! :)

Upvotes: 1

Views: 1001

Answers (3)

Cath
Cath

Reputation: 24074

You can use table on multiple variables:

with(as.data.frame(d), table(ValA, ValB, S.ID))

# , , S.ID = A
#    ValB
#ValA 0 99
#  0  1  1
#  99 0  1

#, , S.ID = B
#    ValB
#ValA 0 99
#  0  1  2
#  99 1  0

Upvotes: 4

Bernhard
Bernhard

Reputation: 4417

Using tapply or by needs conversion to a data.frame:

d<-data.frame(d)
by(d, d$S.ID, function(grouped.d) table(grouped.d$ValA, grouped.d$ValB))

Upvotes: 1

Sandipan Dey
Sandipan Dey

Reputation: 23101

You can try this:

d <- as.data.frame(d)
l <- lapply(unique(d$S.ID), function(x) xtabs(~ValB+ValA, subset(d, S.ID==x)))
names(l) <- unique(d$S.ID)
l
#$A
#    ValA
#ValB 0 99
#  0  1  0
#  99 1  1

#$B
#    ValA
#ValB 0 99
#  0  1  1
#  99 2  0

Upvotes: 0

Related Questions