将长格式更改为宽格式,多个ID和多个值,意外输出(已解决:issue =不同的库)

问题描述 投票:0回答:1

有人可以帮助我在程序中找到错误吗?我有看起来像这样的数据(下面的数据)(尽管实际上有大约15个Var列):

enter image description here

而且我希望它采用宽格式,因此看起来像这样:

enter image description here

为什么此代码不起作用? :(

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

我进行了一些搜索,我最大的猜测是问题与不平衡的设计有关(某些ID-Key组合不会发生),但是我还没有找到合适的解决方案。这不是以下内容的重复: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"))

我得到不想要的输出:

enter image description here

r reshape reshape2
1个回答
1
投票

感谢jay.sf试用代码。在他发表评论之后,我发现问题与加载的软件包有关。在运行上面的代码之前加载tidyverse时,我得到了不想要的输出。现在,我将继续谷歌搜索如何使用重塑功能的基本版本...

© www.soinside.com 2019 - 2024. All rights reserved.