By Anne


2018-03-01 18:45:41 8 Comments

I have data from subjects currently structured like

df <- data.frame(subject.ID = 1:6, 
      questionA = as.factor(c("SA", "A", "D", "SD", "A", "N")), 
      questionB = as.factor(c("A", "A", "D", "SD", "A", "N")), 
      questionC = as.factor(c("SA", "A", "A", "SD", "A", "SD")))

what is the most elegant way to convert it to a data frame with each row including the question and the frequencies of the 5 response options? i.e.

> 
    Question     SA        A       N       D        SD
1   questionA     1        2       1       1        1
2   questionB     0        3       1       1        1
3   questionC     1        3       0       0        2

I've tried working with small subset of the data (just question A) and using plyr's count function before transposing it and relabeling the columns (i.e.:

temp2 <- count(df, c("questionA"))
questionName <- "questionA"
responseNames <- temp2$questionA
temp2 <- as.data.frame(t(temp2[,-1]))
colnames(temp2) <- responseNames
temp2$question <- questionName

It does work for the most part, but it's not ideal (single question at a time which means rbinding all them back together and incredibly clumsy) and it doesn't include the 0 frequency categories. What is a better way of doing this?

2 comments

@akrun 2018-03-02 01:49:34

We can use recast from reshape2 which is a convenient wrapper for melt/dcast

library(reshape2)
recast(df, id.var = "subject.ID", variable ~ value)
#   variable A D N SA SD
#1 questionA 2 1 1  1  1
#2 questionB 3 1 1  0  1
#3 questionC 3 0 0  1  2

@mt1022 2018-03-01 19:05:50

A data.table solution:

library(data.table)
setDT(df)
dcast(melt(df, id.vars = 'subject.ID'), variable ~ value)
#     variable A D N SA SD
# 1: questionA 2 1 1  1  1
# 2: questionB 3 1 1  0  1
# 3: questionC 3 0 0  1  2

Related Questions

Sponsored Content

17 Answered Questions

[SOLVED] Convert data.frame columns from factors to characters

  • 2010-05-17 16:52:02
  • Mike Dewar
  • 567138 View
  • 339 Score
  • 17 Answer
  • Tags:   r dataframe

20 Answered Questions

[SOLVED] Drop data frame columns by name

  • 2011-01-05 14:34:29
  • Btibert3
  • 1397724 View
  • 838 Score
  • 20 Answer
  • Tags:   r dataframe r-faq

20 Answered Questions

[SOLVED] Convert a list to a data frame

  • 2010-11-19 16:40:52
  • Btibert3
  • 714119 View
  • 487 Score
  • 20 Answer
  • Tags:   r list dataframe

10 Answered Questions

[SOLVED] Extracting specific columns from a data frame

  • 2012-04-10 02:24:04
  • Aren Cambre
  • 1079424 View
  • 355 Score
  • 10 Answer
  • Tags:   r dataframe

6 Answered Questions

[SOLVED] Reshape three column data frame to matrix ("long" to "wide" format)

11 Answered Questions

[SOLVED] How to reshape data from long to wide format

  • 2011-05-04 22:27:50
  • Steve
  • 193844 View
  • 251 Score
  • 11 Answer
  • Tags:   r reshape r-faq

5 Answered Questions

[SOLVED] Convert data from long format to wide format with multiple measure columns

  • 2012-05-14 18:33:14
  • colonel.triq
  • 26802 View
  • 34 Score
  • 5 Answer
  • Tags:   r dataframe plyr

16 Answered Questions

[SOLVED] Changing column names of a data frame

  • 2011-05-21 11:31:23
  • Son
  • 1294201 View
  • 381 Score
  • 16 Answer
  • Tags:   r dataframe rename

2 Answered Questions

[SOLVED] Tidying up data in R - should I use loops?

  • 2017-08-08 10:05:52
  • empoleon
  • 47 View
  • 1 Score
  • 2 Answer
  • Tags:   r

1 Answered Questions

[SOLVED] Rearranging a data frame?

  • 2014-06-09 09:51:03
  • ThatGuy
  • 64 View
  • 1 Score
  • 1 Answer
  • Tags:   r

Sponsored Content