I have a data set like this:
structure(list(var1 = c("APE", "APE", "APE", "APE", "APE", "APE", "GIT",
"APE", "APE", "APE", "APE", "APE", "APE", "APE", "GIT"), var2 = c("AVVAL",
"AULASU", "APALA", "AEA", "ATUPVA", "ASATAP", "ADLO", "AKOKU", "AVVAL",
"AULASU", "APALA", "AEA", "ATUPVA", "ASATAP", "ADLO"), var3 = c(NA,
NA, 1L, 101L, 17122009L, 1L, NA, 684L, NA, NA, 1L, 10L, 17122L,
1L, NA)), .Names = c("var1", "var2", "var3"), row.names = c(NA,
15L), class = "data.frame")
How can I reshape this data into wide format? I tried this
reshape(h, idvar="var2", v.names="var3", timevar="var1", direction="wide")
but it is not giving me a correct results. The correct result is:
var1 ADLO AEA AKOKU APALA ASATAP ATUPVA AULASU AVVAL
1 APE NaN 101 NA 1 1 17122009 NA NA
2 APE NaN 10 684 1 1 17122 NA NA
3 GIT NA NaN NaN NaN NaN NaN NaN NaN
4 GIT NA NaN NaN NaN NaN NaN NaN NaN
Edited
The only way I can get to your expected results is to add a new column to the data.frame. It seems to me that there is some information implicit about your data that isn’t contained in the data. In other words, there must be some kind of grouping variable that identifies certain records as belonging together.
Since I can’t double-guess what this information is, in my answer I am going to assume that each occurrence of
GITmarks the end of a record:Original answer
I find the package
reshape2much easier to comprehend than the built-inreshapefunction. This package provides two functions:meltto make a widedata.frametallcastto make a talldata.framewideIn your case you need
cast: