【问题标题】:Reshape long to wide format, multiple IDs and multiple values, unexpected output (solved: issue=different libraries)Reshape long to wide format,多个ID和多个值,意外输出(已解决:问题=不同的库)
【发布时间】:2020-05-11 12:29:10
【问题描述】:

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

我希望它是宽格式的,所以看起来像这样:

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

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

输入(测试LF)

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"))

我得到的不想要的输出:

【问题讨论】:

  • 请考虑我是初学者/显然不是专业程序员。提前道歉,可能会问一个重复的问题,我试图通过使其清晰易读来弥补它........
  • 无法重现这一点,我通过您的代码和输入数据得到了您想要的结果。
  • 哦!谢谢你的尝试!我清除了我的历史记录并再次尝试,它成功了。然后加载了tidyverse,它不再工作了。
  • @Kastany 这可能是由于多个包中可用的同名函数。使用packageName::functionName 定义偏好
  • 谢谢。尽管我再次尝试了相同的步骤并得到了错误的输出,而与使用的包无关。现在重新安装软件包,也许这会有所帮助...

标签: r reshape


【解决方案1】:

感谢 jay.sf 试用代码。在他发表评论后,我发现问题与加载的包有关。

【讨论】:

    猜你喜欢
    • 2022-11-01
    • 2015-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-23
    • 1970-01-01
    • 1970-01-01
    • 2011-08-31
    相关资源
    最近更新 更多