3

I have a data set that looks like this:

ID  wts     S2      S5.1    S5.2    S5.3
42  0.78    Male    Yes     No      Yes
45  1.22    Female  No      Yes     No
48  0.98    Male    Yes     Yes     Yes
49  1.11    Female  Yes     Yes     No
51  1.21    Male    Yes     Yes     No

I am trying to create a weighted table using the 'crosstab' function in the 'descr' package. I've got a basic table working properly with the following line:

crosstab(*fileName*$S5.1,*fileName*$S2,weight=wts,prop.c=T) 

But what I really want to do is have all 3 of the binary variables down the y-axis of the table. I've looked around for days and cannot figure it out. Any help would be appreciated! I am pulling data in through an SPSS file if that info. helps.

Apologies if this is a very simple question. Very new to R.


dput(head(data))


structure(list(ID = c(42, 45, 48, 49, 51), wts = c(0.78, 1.22, 
0.98, 1.11, 1.21), S2 = structure(c(1L, 2L, 1L, 2L, 1L), .Label = c("Male", 
"Female"), class = "factor"), S5.1 = structure(c(2L, 1L, 2L, 
2L, 2L), .Label = c("No", "Yes"), class = "factor"), S5.2 = structure(c(1L, 
2L, 2L, 2L, 2L), .Label = c("No", "Yes"), class = "factor"), 
S5.3 = structure(c(2L, 1L, 2L, 1L, 1L), .Label = c("No", 
"Yes"), class = "factor")), .Names = c("ID", "wts", "S2", 
"S5.1", "S5.2", "S5.3"), variable.labels = structure(c("", "", 
"Gender", "Dog?", "Cat?", "Bird?"), .Names = c("ID", "wts", "S2", 
"S5.1", "S5.2", "S5.3")), codepage = 1252L, row.names = c(NA, 
5L), class = "data.frame")

I was hoping for something resembling this... (where the 2nd and 3rd Yes/No's added are S5.2 and S5.3, respectively). thanks!

==========================================
                  temp.spss$S2
temp.spss$S5.1       Male   Female   Total
------------------------------------------
No                      0        1       1
                    0.000   50.000        
------------------------------------------
Yes                     3        1       4
                  100.000   50.000        
------------------------------------------
No                      1        0       1
                    33.333    00.000        
------------------------------------------
Yes                     2        2       4
                    66.666  100.000        
------------------------------------------
No                      1        2       3
                    33.333   100.000        
------------------------------------------
Yes                     2        0       2
                   66.666    0.000        
------------------------------------------
Total                   9        6       15
                   180.000   120.000
==========================================
Eric W
  • 31
  • 3
  • 1
    Please post `dput(head(data))` in an edit to the question and then show a "right" answer with that data as input. – IRTFM Aug 06 '14 at 21:36
  • A) I don't see the three items on the "y-axis" in your desired output. B) I think predicting that a category has 120% of total (of any marginal) is a bit suspicious. – IRTFM Aug 06 '14 at 22:09
  • the 1st 2 rows in the table are S5.1, the 3rd and 4th rows are S5.2, and 5th and 6th rows are S5.3. Sorry, should have mentioned that earlier. The Total line will equal over 100% b/c we're using multiple questions on the y-axis of the table. – Eric W Aug 07 '14 at 14:25
  • Oh. Then you want a different solution that uses reshaping first. – IRTFM Aug 07 '14 at 16:16

1 Answers1

0

Perhaps using interaction (And guessing you didn't want the lines with all zero entries:

install.packages("descr")
 descr::crosstab( with(dat, 
   interaction(S5.1, S5.2, S5.3, drop=TRUE)) ,dat$S2,weight=dat$wts,prop.c=T)

After looking at the results I see a burning need for review of the results for sensibility.

#+-------------------------------------------------------
   Cell Contents 
|-------------------------|
|                   Count | 
|          Column Percent | 
|-------------------------|

================================================================================
                                                         dat$S2
with(dat, interaction(S5.1, S5.2, S5.3, drop = TRUE))    Female     Male   Total
--------------------------------------------------------------------------------
No.Yes.No                                                     1        0       1
                                                         50.000    0.000        
--------------------------------------------------------------------------------
Yes.Yes.No                                                    1        1       2
                                                         50.000   33.333        
--------------------------------------------------------------------------------
Yes.No.Yes                                                    0        1       1
                                                          0.000   33.333        
--------------------------------------------------------------------------------
Yes.Yes.Yes                                                   0        1       1
                                                          0.000   33.333        
--------------------------------------------------------------------------------
Total                                                         2        3       5
                                                         40.000   60.000
================================================================================
>
IRTFM
  • 258,963
  • 21
  • 364
  • 487