last day (15 days later) » 

02:59
-1
Q: Frequency tables for multiple choice questions across multiple columns but not binary

PreI have a dataframe with multiple choice questions, which have up to 25 different options. For each of these questions the options (from a SurveyMonkey download) get their own column - so there are as many columns as there are options and the string will appear in that column if the person has tic...

Pre
Pre
Thank you for the suggestions - I have updated my original post
Why don't you want to convert it to a binary variable first? It's not difficult to do.... At the very least, you should rename your columns from "Popn1 ... Popn20" to the actual choices they'd contain, then you can use a function like multi.table from the "questionr" package or roll out your own solution with @omri-newett's answer as a starting point.
Pre
Pre
I named them popn1...popn20 etc so I wouldn't have to write out each variable name if I could create an object with the paste function. The package (rdocumentation.org/packages/questionr/versions/0.7.2/topics‌​/…) looks useful for this! thanks for pointing it out. This is also my first big R project so I'm basically swimming against the current here.
@A5C1D2H2I1M1N2O1R2T1 is it true that this package wouldn't work for 2way frequency tables with multiple choice variables? I'd have to do a couple two way tables as well.
@Pre, What would be an example of a 2-way frequency table's input and output from your data? The "questionr" package also has a cross.multi.table function that you might want to look at.
For the column names, if you are sure there would be at least one non-NA value in each column, recreating names shouldn't be tough...
Pre
Pre
@A5C1D2H2I1M1N2O1R2T1 Yes i think few, if any columns are empty. Most have an option. I think i was thinking about the variable names differently, because I was thinking I would use variable labels to make the output meaningful. I actually spent a couple hours creating variable names in Excel, then read them in to align with each column. I read in the excel dataset without the first two obs bc the survey monkey headers are messy. Doesn't the package work with labels?
@A5C1D2H2I1M1N2O1R2T1 How do I convert each of those variables into binomial? researchgate.net/post/… is this the best way?
03:01
@Pre, one way would be to do something like (!is.na(x[cols_of_interest]))+0 (where cols_of_interest would be the columns you want to convert to binary.
Here's an example:
x <- data.frame(id = c("a", "b", "c", "d"), choice1 = c("this", NA, "this", "this"), choice2 = c("that", "that", NA, "that"), choice3 = c("those", "those", "those", "those"), choice4 = c(NA, NA, NA, "these"))
> x
  id choice1 choice2 choice3 choice4
1  a    this    that   those    <NA>
2  b    <NA>    that   those    <NA>
3  c    this    <NA>   those    <NA>
4  d    this    that   those   these
> (!is.na(x[2:5]))+0
     choice1 choice2 choice3 choice4
[1,]       1       1       1       0
[2,]       0       1       1       0
[3,]       1       0       1       0
[4,]       1       1       1       1
The researchgate link you've shared describes what sounds like a very different data structure.
Pre
Pre
yes hat one looks very complicated. this looks more easy to do.
I've just spent about 2 hours trying to understand how frequency tables work in R. Thanks for this.
I'm guessing that the researchgate data looks like this:
> y <- data.frame(id = c("a", "b", "c", "d"), choice = c("this,that,those", "that,those", "this,those", "this,that,those,these"))
> y
  id                choice
1  a       this,that,those
2  b            that,those
3  c            this,those
4  d this,that,those,these
And from there, they're creating a binary matrix.
If your data's like that, you can use my splitstackshape package to get the binary data:
> library(splitstackshape)
> cSplit_e(y, "choice", type="character", fill = 0)
  id                choice choice_that choice_these choice_this choice_those
1  a       this,that,those           1            0           1            1
2  b            that,those           1            0           0            1
3  c            this,those           0            0           1            1
4  d this,that,those,these           1            1           1            1
Pre
Pre
No, the data is like the first one you mentioned
OK. You'll get much faster help here on Stackoverflow if you share reproducible examples like I've done here in the chat. That's what most of the people answering questions expect.
Good luck!
Pre
Pre
Thanks so much!
Pre
Pre
03:31
Just one question - is it possible to do this directly in the large dataset I have, or should I move the variables into their own dataframe? I know I'm able to select columns, but am I able to say something like (!is.na(x[popn1-popn20]))+0
The main dataframe I have has about 320 variables (because there are a few more multiple choice questions in them as well)

last day (15 days later) »