【问题标题】:R Sparklines package errorR迷你图包错误
【发布时间】:2015-06-01 22:27:18
【问题描述】:

我在 R 中使用 Sparklines 包并使用 reshapeExt() 函数准备数据集,但遇到了一个非常非常奇怪的问题。数据集如下所示:

      Company time value
1   Microsoft 1990    11
2 Time Warner 1990    22
3     Verizon 1990    33
4   Microsoft 1991    44
5 Time Warner 1991    55
6     Verizon 1991    66
7   Microsoft 1992    77
8 Time Warner 1992    88
9     Verizon 1992    99

然后我按照教程示例运行这些行:

example <- example[,c("Company","value","time")]
example$time <- as.numeric ( as.character ( example$ time ))
dat <- reshapeExt ( example , idvar =" Company " , varying = list (2))

莫名其妙地,R 给了我这个“dat”:

      Company time value  Company 
1   Microsoft    1    11         1
2 Time Warner    1    22         2
3     Verizon    1    33         3
4   Microsoft    1    44         4
5 Time Warner    1    55         5
6     Verizon    1    66         6
7   Microsoft    1    77         7
8 Time Warner    1    88         8
9     Verizon    1    99         9

时间列无缘无故全部变为 1。当我在第 18 页上实现 https://web.warwick.ac.uk/statsdept/user2011/TalkSlides/Contributed/18Aug_0950_FocusVI_4-ReportingData_2-Kowarik.pdf 的示例时,这不会发生 - 这里发生了什么?

感谢任何帮助

【问题讨论】:

    标签: r time-series reshape sparklines sparktable


    【解决方案1】:

    8 个月后,我不知道它是否对您有用,但是,我遇到了类似的问题,我找到了您的问题。也许这无论如何都会对其他人有所帮助。

    首先,确认您只有 3 列按此顺序: 公司、时间、价值

    我认为问题是因为数据的顺序,你需要按公司和时间排序:

    example <- examplewith(example, order(Company, time)), ]
    

    那么数据会是这样的:

          Company time value
    1   Microsoft 1990    11
    4   Microsoft 1991    44
    7   Microsoft 1992    77
    2 Time_Warner 1990    22
    5 Time_Warner 1991    55
    8 Time_Warner 1992    88
    3     Verizon 1990    33
    6     Verizon 1991    66
    9     Verizon 1992    99
    

    然后使用 reshapeExt,并将 varying = list(3) 更改为 3,因为您的值在第三列中。

    example2 <- reshapeExt(example, idvar = "Company", varying = list(3))
    

    最终结果是:

          Company time value
    1   Microsoft    1    11
    4   Microsoft    2    44
    7   Microsoft    3    77
    2 Time_Warner    1    22
    5 Time_Warner    2    55
    8 Time_Warner    3    88
    3     Verizon    1    33
    6     Verizon    2    66
    9     Verizon    3    99
    

    我认为会以这种方式工作。 谢谢!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-14
      • 2017-12-30
      • 1970-01-01
      • 1970-01-01
      • 2021-03-06
      • 2016-05-27
      • 1970-01-01
      • 2017-09-01
      相关资源
      最近更新 更多