0

I have a data.frame in R that looks like this:

df <- data.frame(date=c(1,2,1,2), id=c(11,11,22,22), x=c(1,2,3,4))

But I want to rearrange it with a two new columns (depending on which id is considered) as

df2 <- data.frame(date=c(1,2),x1=c(1,2),x2=c(3,4))

How can do it? What is the general approach to such tasks?

Claudio Moneo
  • 489
  • 1
  • 4
  • 10

1 Answers1

0

Using reshape.

reshape(df, direction="wide", timevar="id", idvar="date")
#   date x.11 x.22
# 1    1    1    3
# 2    2    2    4
jay.sf
  • 60,139
  • 8
  • 53
  • 110