Reshape long to wide format, multiple IDs and multiple values, unexpected output (solved: issue=different libraries)

Viewed 102

Could someone help me find the mistake in my program? I have data (dput below) that looks like this (though in reality with about 15 Var columns):

enter image description here

And I want it to be in wide format, so to look like this:

enter image description here

Why doesn't this code work? :(

Wf <- reshape(data=testLF,
                          idvar=c("ID","ID2"), 
                          timevar = "Key",
                          direction="wide")

I have done some searching and my best guess is that the issue has to do with the unbalanced design (some ID-Key combinations do not occur), but I havent found a fitting solution. This is not a duplicate of: Reshape data from long to wide with multiple measure columns using spread() or other reshape functions

dput(testLF)

structure(list(ID = c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L), ID2 = c("a", 
"a", "b", "b", "c", "c", "d", "d"), Key = c("A", "B", "A", "C", 
"A", "B", "A", "C"), Var1 = c(1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L), 
    Var2 = c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), Var3 = c(0L, 4L, 
    5L, 0L, 5L, 4L, 0L, 5L)), row.names = c(NA, -8L), class = c("tbl_df", 
"tbl", "data.frame"), spec = structure(list(cols = list(ID = structure(list(), class = c("collector_integer", 
"collector")), ID2 = structure(list(), class = c("collector_character", 
"collector")), Key = structure(list(), class = c("collector_character", 
"collector")), Var1 = structure(list(), class = c("collector_integer", 
"collector")), Var2 = structure(list(), class = c("collector_integer", 
"collector")), Var3 = structure(list(), class = c("collector_integer", 
"collector"))), default = structure(list(), class = c("collector_guess", 
"collector"))), class = "col_spec"))

THe undesired output I get:

enter image description here

1 Answers

Thanks to jay.sf for trying out the code. After his comment, I discovered the issue had to do with loaded packages.

Related