2

That's my data:

> head(data)

    id C1 C2 C3 B1 B2 B3 Name
    12 3  12 8  1  3  12 Agar
    14 4  11 9  5  12 14 LB
    18 7  17 6  7  14 16 YEF
    20 9  15 4  3  11 17 KAN

so I used a melt function from reshape2 package to reorganize my data. Now it looks like that:

dt <- melt(data, measure.vars=2:7)

> head(dt)

   n    v variable value   rt
1 id Name        p    C1   1
2 12 Agar        p     3   2
3 14   LB        p     4   3
4 18  YEF        p     7   6
5 20  KAN        p     9   3
6 id Name        u    C2   1

I did some calculations on my data and now there is an extra column. Let's call it "rt". I'd like to transform my data now to the previous "state" with this an extra column. Do you know any function which would be useful ?

dput(dt)
structure(list(n = structure(c(5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 
3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 
4L, 5L, 1L, 2L, 3L, 4L), class = "factor", .Label = c("12", "14", 
"18", "20", "id")), v = structure(c(4L, 1L, 3L, 5L, 2L, 4L, 1L, 
3L, 5L, 2L, 4L, 1L, 3L, 5L, 2L, 4L, 1L, 3L, 5L, 2L, 4L, 1L, 3L, 
5L, 2L, 4L, 1L, 3L, 5L, 2L), class = "factor", .Label = c("Agar", 
"KAN", "LB", "Name", "YEF")), variable = structure(c(1L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 
4L, 4L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L), .Label = c("p", 
"u", "k", "l", "t", "h"), class = "factor"), value = c("C1", 
"3", "4", "7", "9", "C2", "12", "11", "17", "15", "C3", "8", 
"9", "6", "4", "B1", "1", "5", "7", "3", "B2", "3", "12", "14", 
"11", "B3", "12", "14", "16", "17")), .Names = c("n", "v", "variable", 
"value"), row.names = c(NA, -30L), class = "data.frame")
A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485
Shaxi Liver
  • 1,052
  • 3
  • 25
  • 47
  • 3
    The function that would be useful would probably be `dcast`, but you need to provide a reproducible example if you want meaningful help. – A5C1D2H2I1M1N2O1R2T1 Oct 28 '13 at 11:36
  • 1
    [Please see this link](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Giving proper reproducible example will likely get better and faster answers. – CHP Oct 28 '13 at 11:36
  • Could you tell me what exactly is missing in my example ? I tried to follow all of the rules so: minimal dataset is given, the package is mentioned. If you mean about runnable code so I don't have any idea which function should I use. Let's try this dcast then :). – Shaxi Liver Oct 28 '13 at 11:44
  • 1
    @ShaxiLiver, to create reproducible code, use `dput` or manually create a small example. – A5C1D2H2I1M1N2O1R2T1 Oct 28 '13 at 11:46

1 Answers1

7

In the "reshape2" universe, melt and *cast go hand-in-hand.

Here's an example of melting a data.frame and dcasting it back to its original form. You would need to take a similar approach with your data.

mydf <- data.frame(A = LETTERS[1:3], B = 1:3, C = 4:6)
mydf
#   A B C
# 1 A 1 4
# 2 B 2 5
# 3 C 3 6

library(reshape2)

mDF <- melt(mydf, id.vars="A")
mDF
dcast(mDF, A ~ variable, value.var="value")
#   A B C
# 1 A 1 4
# 2 B 2 5
# 3 C 3 6

In the dcast step, think of the items before the ~ as being the "id" variables, and those coming after as being the resulting column names. value.var should be the column from which the values will fill in the resulting "grid" created by the id variables and column names.

A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485
  • Yeah, that's what I am looking for. Now I have to only try to fit it to my data set, which it's not so easy. – Shaxi Liver Oct 28 '13 at 12:02
  • @ShaxiLiver, I think I see your problem quite easily. It appears that you melted a dataset that had column names as the first row. Still, you get most of the way by just doing: `dcast(dt, n + v ~ variable, value.var="value")` – A5C1D2H2I1M1N2O1R2T1 Oct 28 '13 at 12:06
  • Yup, that's it exactly. The last problem is that the last row of the new dataset should be the first one to be precisly. Any idea how to solve it ? – Shaxi Liver Oct 28 '13 at 12:13
  • Here are two options, assuming we start with `out <- dcast(dt, n + v ~ variable, value.var="value")`. (1) Just rearrange the rows as required: `out[c(5, 1:4), ]`. (2) Actually create real column names: `setNames(out[-5, ], sapply(out[5, ], as.character))`. – A5C1D2H2I1M1N2O1R2T1 Oct 28 '13 at 12:17