0

I would like to turn a dataframe column with unique identifiers into multiple rows (one row for each unique identifier)

Example:

V1    V2    Unique
5     12      1
10    15      1
20    35      1
40    50      2
60    70      2
100   50      2

What I need is:

row1 5 10 20
row2 40 60 100

Essentially I would like a row of V1 (ordering from left to right for first to last match) when Unique is the same (ignoring V2 entirely)

Thanks in advance.

FXQuantTrader
  • 6,821
  • 3
  • 36
  • 67
user3272284
  • 279
  • 2
  • 3
  • 10
  • are those always of same length? ie 3 in this case? otherwise you would be adding extra columns..or may be you want list or vector id that is of different length? – Ananta Feb 04 '14 at 23:20

1 Answers1

1

For example using unstack , if all groups have the same length:

t(unstack(dat[,c('V1','Unique')]))
   [,1] [,2] [,3]
X1    5   10   20
X2   40   60  100

Or if they don't have the same length, you can use split:

split(dat$V1,dat$Unique)
$`1`
[1]   5  10  20 100

$`2`
[1] 40 60
agstudy
  • 119,832
  • 17
  • 199
  • 261