我怎样才能以
的形式转换时间序列矩阵 dDomingo dSegunda dTerça dQuarta dQuinta dSexta dSábado
[1,] -1.783558 0.5123224 0.5997068 0.4680476 0.6521374 0.6276698 -1.0763264
[2,] -1.730086 0.8552685 0.6426731 0.6377859 0.3995325 0.2944567 -1.0996309
[3,] -1.503333 0.9450167 -0.4125656 0.8446433 0.6839187 0.5397113 -1.0973919
[4,] -1.720261 0.3044134 0.6722075 0.5313041 0.6411365 0.7105043 -1.1393050
[5,] -1.234701 0.9189446 0.9670409 0.8713827 -1.1208727 0.3465987 -0.7483936
进入这个
V1 V2
[1,] -1.783558 dDomingo
[2,] 0.5123224 dSegunda
[3,] 0.5997068 dTerça
[4,] 0.4680476 dQuarta
[5,] 0.6521374 dQuinta
[6,] 0.6276698 dSexta
[7,] -1.0763264 dSábado
[8,] -1.730086 dDomingo
......等等,直到
[35,] -0.7483936 dSábado
请注意,我想要的新数据格式只有两列。第一个将包含最后一个矩阵的变量值。第二列将包含变量值为的相应列。
时间序列矩阵数据如下:
structure(c(-1.78355763661757, -1.73008589314791, -1.50333251627705,
-1.72026078275706, -1.23470060199216, 0.512322374645035, 0.855268450002498,
0.945016691336727, 0.304413384239457, 0.91894460806156, 0.599706795271625,
0.642673149573306, -0.412565627467277, 0.672207513790216, 0.967040903075167,
0.468047601527563, 0.637785901287577, 0.844643267672228, 0.531304065337568,
0.871382716325882, 0.652137447647579, 0.39953254735831, 0.683918734842367,
0.64113649695194, -1.12087270379329, 0.627669809872133, 0.294456709215146,
0.539711347678816, 0.710504348497859, 0.346598697399641, -1.07632639234637,
-1.09963086428893, -1.09739189778582, -1.13930502605998, -0.748393619076803
), .Dim = c(5L, 7L), .Dimnames = list(NULL, c("dDomingo", "dSegunda",
"dTerça", "dQuarta", "dQuinta", "dSexta", "dSábado")))
答案 0 :(得分:2)
在此基本代码末尾的注释中使用
alert('<?php echo $cat_name; ?>');
alert('<?php echo $cat_name; ?>');
或者
将创建一个包含35行的双列数据框;但请注意,m
将使用ggplot2绘制多变量动物园时间序列,而不需要任何显式变换。如果您希望每个系列都在一个单独的面板中,请删除stack(as.data.frame(m))
。有关详细信息和示例,请参阅library(zoo)
fortify.zoo(zoo(m), melt = TRUE)
和autoplot.zoo
。
facet = NULL
我们假设输入?autoplot.zoo
是:
?fortify.zoo
答案 1 :(得分:1)
您需要先将matrix
转换为data frame
。然后使用tidyr::gather
转换为长格式
mat1 <- structure(c(-1.78355763661757, -1.73008589314791, -1.50333251627705,
-1.72026078275706, -1.23470060199216, 0.512322374645035, 0.855268450002498,
0.945016691336727, 0.304413384239457, 0.91894460806156, 0.599706795271625,
0.642673149573306, -0.412565627467277, 0.672207513790216, 0.967040903075167,
0.468047601527563, 0.637785901287577, 0.844643267672228, 0.531304065337568,
0.871382716325882, 0.652137447647579, 0.39953254735831, 0.683918734842367,
0.64113649695194, -1.12087270379329, 0.627669809872133, 0.294456709215146,
0.539711347678816, 0.710504348497859, 0.346598697399641, -1.07632639234637,
-1.09963086428893, -1.09739189778582, -1.13930502605998, -0.748393619076803
), .Dim = c(5L, 7L), .Dimnames = list(NULL, c("dDomingo", "dSegunda",
"dTerça", "dQuarta", "dQuinta", "dSexta", "dSábado")))
df <- data.frame(mat1)
library(tidyr)
df %>%
gather(key, value)
#> key value
#> 1 dDomingo -1.7835576
#> 2 dDomingo -1.7300859
#> 3 dDomingo -1.5033325
#> 4 dDomingo -1.7202608
#> 5 dDomingo -1.2347006
#> 6 dSegunda 0.5123224
#> 7 dSegunda 0.8552685
#> 8 dSegunda 0.9450167
#> 35 dSábado -0.7483936
# rename to whatever you want
df %>%
gather(key = "V1", value = "V2")
#> V1 V2
#> 1 dDomingo -1.7835576
#> 2 dDomingo -1.7300859
#> 3 dDomingo -1.5033325
#> 4 dDomingo -1.7202608
#> 5 dDomingo -1.2347006
#> 6 dSegunda 0.5123224
#> 7 dSegunda 0.8552685
#> 8 dSegunda 0.9450167
#> 35 dSábado -0.7483936
修改:要返回wide
格式,请使用tidyr::spread
library(dplyr)
df_long %>%
group_by(key) %>%
mutate(rowid = row_number()) %>%
spread(key, value) %>%
select(-rowid)
dDomingo dSegunda dTerça dQuarta dQuinta dSexta dSábado
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 -1.78 0.512 0.600 0.468 0.652 0.628 -1.08
2 -1.73 0.855 0.643 0.638 0.400 0.294 -1.10
3 -1.50 0.945 -0.413 0.845 0.684 0.540 -1.10
4 -1.72 0.304 0.672 0.531 0.641 0.711 -1.14
5 -1.23 0.919 0.967 0.871 -1.12 0.347 -0.748
由reprex package(v0.2.0)创建于2018-04-15。
答案 2 :(得分:-1)
t(你的结构在这里)
t(structure(c(-1.78355763661757, -1.73008589314791, -1.50333251627705,
-1.72026078275706, -1.23470060199216, 0.512322374645035, 0.855268450002498,
0.945016691336727, 0.304413384239457, 0.91894460806156, 0.599706795271625,
0.642673149573306, -0.412565627467277, 0.672207513790216, 0.967040903075167,
0.468047601527563, 0.637785901287577, 0.844643267672228, 0.531304065337568,
0.871382716325882, 0.652137447647579, 0.39953254735831, 0.683918734842367,
0.64113649695194, -1.12087270379329, 0.627669809872133, 0.294456709215146,
0.539711347678816, 0.710504348497859, 0.346598697399641, -1.07632639234637,
-1.09963086428893, -1.09739189778582, -1.13930502605998, -0.748393619076803
), .Dim = c(5L, 7L), .Dimnames = list(NULL, c("dDomingo", "dSegunda",
"dTerça", "dQuarta", "dQuinta", "dSexta", "dSábado"))))
输出
[,1] [,2] [,3] [,4] [,5]
dDomingo -1.7835576 -1.7300859 -1.5033325 -1.7202608 -1.2347006
dSegunda 0.5123224 0.8552685 0.9450167 0.3044134 0.9189446
dTerça 0.5997068 0.6426731 -0.4125656 0.6722075 0.9670409
dQuarta 0.4680476 0.6377859 0.8446433 0.5313041 0.8713827
dQuinta 0.6521374 0.3995325 0.6839187 0.6411365 -1.1208727
dSexta 0.6276698 0.2944567 0.5397113 0.7105043 0.3465987
dSábado -1.0763264 -1.0996309 -1.0973919 -1.1393050 -0.7483936