I would like to reshape a data.frame that looks like this:
permno dte ttm var1 var2 var3
1 123 2012-01-01 20 1 10 100
2 123 2012-01-01 30 -1 10 100
3 124 2012-01-01 20 2 20 200
4 124 2012-01-01 30 -2 20 200
I would like to make my data.frame look the following way:
permno dte var1_20 var1_30 var2 var3
1 123 2012-01-01 1 -1 10 100
2 124 2012-01-01 2 -2 20 200
I have been attempting to do this with reshape2 package but I am unable to isolate var1 from the rest and keep getting var2_20 and var2_30 for example in the results. Does anyone know how to do this using the reshape2 package?
data.frame dput:
> dput(DF)
structure(list(permno = c(123L, 123L, 124L, 124L), dte = structure(c(1L,
1L, 1L, 1L), .Label = " 2012-01-01", class = "factor"), ttm = c(20L,
30L, 20L, 30L), var1 = c(1L, -1L, 2L, -2L), var2 = c(10L, 10L,
20L, 20L), var3 = c(100L, 100L, 200L, 200L)), .Names = c("permno",
"dte", "ttm", "var1", "var2", "var3"), class = "data.frame", row.names = c(NA,
-4L))
> dput(result)
structure(list(permno = 123:124, dte = structure(c(1L, 1L), .Label = " 2012-01-01", class = "factor"),
var1_20 = 1:2, var1_30 = c(-1L, -2L), var2 = c(10L, 20L),
var3 = c(100L, 200L)), .Names = c("permno", "dte", "var1_20",
"var1_30", "var2", "var3"), class = "data.frame", row.names = c(NA,
-2L))
Use a combination of
merge,reshape, anduniqueas follows:Basically, you reshape only the columns that need to be reshaped, and drop those columns from the original dataset before merging. You’ll end up with duplicated rows, so just wrap all of that in
uniqueto get (almost) your desired output. You can rearrange the column order if required.