0

The data that I have on R is of the following type:

Data = data.frame(Individual = c(1,1,2,2), Year=c(2000,2001, 2000,2001), Variable=c('x','x','y','y'), Value=c(1,2,3,4))

  Individual Year Variable Value
1          1 2000        x     1
2          1 2001        x     2
3          1 2000        y     3
4          1 2001        y     4

I have data by individuals, by year, for 2 variables, x and y. What I wish is to reshape the data wide. In other words, I would like to, instead of having 4 observations (2 per person), have just two observations, but instead of concatenating x and y, have them as separate columns.

Ideally, the data would look like:

  Individual Year x y
1          1 2000 1 3
2          1 2001 2 4

I am new to R, and am making use of the wonderful dplyr() package. I have come across the sep() functions, but that does not seem to be exactly what I am looking for. Any help on this is much appreciated.

ChinG
  • 125
  • 7
  • 2
    Does this answer your question? [How to reshape data from long to wide format](https://stackoverflow.com/questions/5890584/how-to-reshape-data-from-long-to-wide-format) – desval Jun 10 '20 at 23:41
  • Perfectly. Thank you. – ChinG Jun 10 '20 at 23:42
  • Your assignment *to* `X` is different from alleged representation of `X` ... the individuals are different. Please be clear in what you have ... – r2evans Jun 10 '20 at 23:42
  • 1
    Assuming your data looks like the formatted `X`, and not the code creating `X`, then: `tidyr::pivot_wider(X, names_from="Variable", values_from="Value")`. – r2evans Jun 10 '20 at 23:43
  • @r2evans My mistake. I have made the requisite changes (conditional on understanding your comment) . – ChinG Jun 10 '20 at 23:46
  • If you would post this as answer, I can accept it. – ChinG Jun 10 '20 at 23:46
  • It's a dupe, so no answer required (since you already got it, anyway), but the issue with the data is that you define `Individual = c(1,1,2,2)` yet your frame shows all 1s. – r2evans Jun 10 '20 at 23:47

0 Answers0