I can't seem to reshape one column of my data frame into the correct shape while retaining unique identifiers for each row. I have the following data
id x y indicator
1 1 249.6 1.124985 1
2 1 250.9 1.124756 1
3 1 252.2 1.124125 1
4 1 253.5 1.124598 1
5 1 254.8 1.127745 1
6 1 256.1 1.129102 1
7 2 249.6 2.167348 0
8 2 250.9 2.165804 0
9 2 252.2 2.164578 0
10 2 253.5 2.163828 0
11 2 254.8 2.164260 0
12 2 256.1 2.166293 0
13 3 249.6 0.04647765 0
14 3 250.9 0.04932262 0
15 3 252.2 0.05245448 0
15 3 253.5 0.05692405 0
17 3 254.8 0.06184551 0
18 3 256.1 0.06751989 0
I would like to reshape the y vector into a matrix where each row corresponds to a single y vector and there are additional columns for id and indicator, while the variable columns are labeled by the x values. Something like this:
id indicator 249.6 250.9 252.2 ...
1 1 1.124985 1.124756 1.124125 ...
2 0 2.167348 2.165804 2.164578 ...
3 0 0.04647765 0.04932262 0.05245448 ...
I've tried using the reshape function like this:
reshape(df[c('id','x','y')], direction = "wide", idvar = "id", timevar = "x")
In this case, I was just ignoring the indicator variable to see if it would work, but I got a data frame with only two columns back, the first column was ID, and the second was y.c(249.6, 250.9, 252.2, 253.5, 254.8, 256.1, 257.4, 258.7, 260, 261.3, 262.6, 263.9, 265.2, 266.5, 267.8, 269.1, 270.4, 271.7, 273, 274.3, 275.6, 276.9, 278.2, 279.5, 280.8, 282.1, 283.4, 284.7, 286, 287.3, 288.6, 289.9, 291.2, 292.5, 293.8, 295.1,[etc]
.
I also tried using the xtabs
function: a = xtabs(formula = y ~ id + indicator+ x, data=df)
, but this just returned a table that looks very similar to the one I have as input.