0

Suppose you have a wide dataset, where someone is asked to rate various product on the same attributes.

id represents a given individual

X1 represents the various products the person is asked to rate.

eq_ and rate_ represent the various attributes they are asked to rate by. For example, eq_1 is one attribute, rate_2 is a different one, and so on.

    id X1.eq_1 X1.eq_2 X2.eq_1 X2.eq_2 X3.eq_1 X3.eq_2 X1.rate_1 X1.rate_2 X2.rate_1 X2.rate_2 X3.rate_1 X3.rate_2
1   1       2       5       2       1       2       1         5         2         1         2         2         3
2   2       1       1       2       3       3       3         1         4         4         1         3         3
3   3       5       3       2       2       5       3         3         5         1         1         2         5
4   4       3       2       4       4       3       1         3         5         5         1         2         5
5   5       2       3       1       5       4       3         5         5         1         5         2         4
6   6       3       5       4       5       1       4         4         5         4         4         3         5
7   7       5       5       4       1       5       1         2         4         5         3         2         2
8   8       4       4       2       4       1       2         4         4         5         4         3         4
9   9       4       5       3       1       2       1         4         4         3         5         4         4
10 10       5       4       1       4       1       4         1         3         5         2         1         3

Ultimately I'd like to end up with something like this -- note I blanked out the data because I'm not confident I can get the results to look right.

id  product eq_1    eq_2    rate_1  rate_2
1   1   (data)  (data)  (data)  (data)
2   1   (data)  (data)  (data)  (data)
3   1   (data)  (data)  (data)  (data)
4   1   (data)  (data)  (data)  (data)
5   1   (data)  (data)  (data)  (data)
6   1   (data)  (data)  (data)  (data)
7   1   (data)  (data)  (data)  (data)
8   1   (data)  (data)  (data)  (data)
9   1   (data)  (data)  (data)  (data)
10  1   (data)  (data)  (data)  (data)
1   2   (data)  (data)  (data)  (data)
2   2   (data)  (data)  (data)  (data)
3   2   (data)  (data)  (data)  (data)
4   2   (data)  (data)  (data)  (data)
5   2   (data)  (data)  (data)  (data)
6   2   (data)  (data)  (data)  (data)
7   2   (data)  (data)  (data)  (data)
8   2   (data)  (data)  (data)  (data)
9   2   (data)  (data)  (data)  (data)
10  2   (data)  (data)  (data)  (data)

Here is the dput of the data:

structure(list(id = 1:10, X1.eq_1 = c(2L, 1L, 5L, 3L, 2L, 3L, 
5L, 4L, 4L, 5L), X1.eq_2 = c(5L, 1L, 3L, 2L, 3L, 5L, 5L, 4L, 
5L, 4L), X2.eq_1 = c(2L, 2L, 2L, 4L, 1L, 4L, 4L, 2L, 3L, 1L), 
    X2.eq_2 = c(1L, 3L, 2L, 4L, 5L, 5L, 1L, 4L, 1L, 4L), X3.eq_1 = c(2L, 
    3L, 5L, 3L, 4L, 1L, 5L, 1L, 2L, 1L), X3.eq_2 = c(1L, 3L, 
    3L, 1L, 3L, 4L, 1L, 2L, 1L, 4L), X1.rate_1 = c(5L, 1L, 3L, 
    3L, 5L, 4L, 2L, 4L, 4L, 1L), X1.rate_2 = c(2L, 4L, 5L, 5L, 
    5L, 5L, 4L, 4L, 4L, 3L), X2.rate_1 = c(1L, 4L, 1L, 5L, 1L, 
    4L, 5L, 5L, 3L, 5L), X2.rate_2 = c(2L, 1L, 1L, 1L, 5L, 4L, 
    3L, 4L, 5L, 2L), X3.rate_1 = c(2L, 3L, 2L, 2L, 2L, 3L, 2L, 
    3L, 4L, 1L), X3.rate_2 = c(3L, 3L, 5L, 5L, 4L, 5L, 2L, 4L, 
    4L, 3L)), .Names = c("id", "X1.eq_1", "X1.eq_2", "X2.eq_1", 
"X2.eq_2", "X3.eq_1", "X3.eq_2", "X1.rate_1", "X1.rate_2", "X2.rate_1", 
"X2.rate_2", "X3.rate_1", "X3.rate_2"), class = "data.frame", row.names = c(NA, 
-10L))

EDIT: this isn't answered by any of the potential duplicates, because all of the duplicates are using hard-coding for the variable names. There must be a way to automate this? My dataset has over 100 variables, so there's no way I can hard code all of that.

vashts85
  • 897
  • 2
  • 12
  • 26
  • 1
    I can see a difference between your question and the supposed duplicate, unfortunately I can no longer answer. But I think your problem can be solved by first melting and then reshaping again into wide format. Apologies for the messy format. `library(reshape2); data2 – Niek Jul 18 '17 at 15:09
  • What exactly is `var2` providing? I think this works, but it's unfortunate I can't mark it as an asnwer. – vashts85 Jul 18 '17 at 19:01
  • 1
    `var2` provides the `rate_#` and `eq_#` attributes. Unfortunate indeed but no worries, glad to have helped :) – Niek Jul 19 '17 at 07:00

0 Answers0