2

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

Kastany
  • 427
  • 1
  • 5
  • 16
  • please consider that I am a beginner / obviously not a professional programmer. Apologies in advance for maybe asking a duplicate question, I tried to make up for it by making it clear and easy to look at........ – Kastany May 11 '20 at 12:30
  • 1
    Can't reproduce this, I get exactly your desired result with your code and dput data. – jay.sf May 11 '20 at 12:39
  • 1
    oh! thank you for trying! I cleared my history and tried again, it worked. Then loaded tidyverse, and it did not work anymore. – Kastany May 11 '20 at 12:43
  • 1
    @Kastany this might be due to the same name function available in more than packages. Define preference using `packageName::functionName` – rj-nirbhay May 11 '20 at 12:50
  • Thank you. Although I tried the same steps again and got the wrong output irrespective of package used. Reinstalling packages now, perhaps that will help... – Kastany May 11 '20 at 12:53

1 Answers1

1

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

Kastany
  • 427
  • 1
  • 5
  • 16