0

Is there a way to change the default way that the dcast function names variables? For example

require(reshape2)
x = data.frame(id=1:2, t=1:5, v=10:1)
m = melt(x, id.vars = c("id", "t"))
cx = dcast(m,  t ~ variable + id)
print(cx)

#  t v_1 v_2
#1 1  10   5
#2 2   4   9
#3 3   8   3
#4 4   2   7
#5 5   6   1

I would like v_1 named v_id_1 or something.

Alex
  • 19,533
  • 37
  • 126
  • 195

1 Answers1

2

I don't think it is possible using dcast but you can use gsub like this :

 colnames(cx) <- gsub('(.*)_(*.)','\\1_id_\\2',colnames(cx))
> cx
  t v_id_1 v_id_2
1 1     10      5
2 2      4      9
3 3      8      3
4 4      2      7
5 5      6      1
agstudy
  • 119,832
  • 17
  • 199
  • 261