-1

I have a data set as following, in which each ID has multiple rows for different attributes.

 ID<-c(1,1, 2,2,3,3)
 Score<-c(4,5, 5,7,8,9)
 Attribute<-c("Att_1","Att_2", "Att_1","Att_2",   "Att_1","Att_2")
T<-data.frame(ID, Score, Attribute) 

Need to transform it to following format so each ID has one row:

 ID  Att_1   Att_2
 1    4        5
 2    5        7
 3    8        9

There are threads on how to do this in excel, just wondering is there is any neat way to do in R? Thanks a lot!

D Jay
  • 99
  • 1
  • 3
  • 12

2 Answers2

2

You could try this:

library(reshape2)
dcast(T, ID ~ Attribute, value.var="Score")
#  ID Att_1 Att_2
#1  1     4     5
#2  2     5     7
#3  3     8     9
RHertel
  • 23,412
  • 5
  • 38
  • 64
1

This can be done with reshape():

reshape(data.frame(ID,Score,Attribute),idvar='ID',timevar='Attribute',dir='w');
##   ID Score.Att_1 Score.Att_2
## 1  1           4           5
## 3  2           5           7
## 5  3           8           9
bgoldst
  • 34,190
  • 6
  • 38
  • 64