Multiple variables on one side of a crosstab in R
I have a dataset 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 have a basic table working correctly with the following line:
crosstab(*fileName*$S5.1,*fileName*$S2,weight=wts,prop.c=T)
But what I really want to do is all 3 binary variables down the y-axis of the table. I looked around for several days and could not understand. Any help would be appreciated! I am collecting data through an SPSS file if it is information. helps.
Sorry 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 similar to this ... (where 2nd and 3rd yes / no S5.2 and S5.3 are added, respectively). thank!
==========================================
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
==========================================
source to share
Perhaps using interop (And guessing that you don't want strings with all null 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 reviewing the results, I see a burning need to review the results for sensitivity.
#+-------------------------------------------------------
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
================================================================================
>
source to share