0

I have data from a survey. I have data from several Likert-type questions. For example: "To what extent did the following factors contribute to your decision to enroll in a PhD program?" with 4 categories: 1: Not at all; 2: To a small extent; 3: To some extent; 4: To a great extent)

I exported the data and it currently looks like this:

id  Q1_Item1  Q1_Item2  Q1_Item3  Q1_Item4
 1         4         4         4         2
 2         1         2         3         4
 3         3         4         4         4
 4         3         3         3         3
 5         2         1         1         1

I want to tabulate the data so it looks like this

      Not at all  To a small extent  To some extent  To a great extent 
Item1          1                  1               2                  1
Item2          1                  1               1                  2
Item3          1                  0               2                  2
Item4          1                  1               1                  2

where the number now represent the counts of responses under each category. How can I do this in R?

Bartolome Salom
  • 103
  • 2
  • 5
  • OK, so what is your question? What have you tried? Where do you get stuck? – Andrie Jul 04 '12 at 12:52
  • Andrie, this is actually my first question on Stackoverflow. I was trying to figure out how to format data to post here. I now realize that I posted before it was complete, but I've now edited it. Cheers, – Bartolome Salom Jul 06 '12 at 00:07
  • You've modified your data significantly, and now have some entries that are larger than the count of levels ... ? You need to examine your problem statement more thoroughly. – IRTFM Jul 06 '12 at 14:58

1 Answers1

4

Assuming these have been read in as factors with those text entries as labels, then this works:

# If the data was read in as numeric, then this will convert to factor-class
dfrm[-1] <- lapply(dfrm[-1], factor, levels=1:4, labels=c("Not at all", 
                        "To a small extent", "To some extent", "To a great extent") )
t( sapply(dfrm[-1], table) )
        Not at all To a small extent To some extent To a great extent
factor1          2                 1              4                 3
factor2          0                 0              3                 8
factor3          0                 0              3                 9
factor4          0                 0              6                 6
factor5          0                 0              3                 8
factor6          2                 0              0                10
factor7          0                 0              2                10
IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • DWin, do you know if it is possible to export the table using xtable or other package? I've tried to do it using xtable with no luck. – Bartolome Salom Jul 10 '12 at 03:12
  • I'm not at computer that can run R, so I cannot test my ideas. The output of that function is a matrix, so any output that handles a matrix would work. Similarly if you had a method that worked with a data.frame, you could wrap it in `as.data.frame`. – IRTFM Jul 10 '12 at 17:27