1. ホーム
  2. r

[解決済み] 非常によく似た2つのセットのうちの1つで、「'dimnames' [2]の長さが配列の範囲と等しくない」。

2022-01-30 09:27:02

質問

2つのデータフレームでデータを収集しました。 volsflow . 後者は行列に変換できますが、前者は変換できません。

as.matrix(vols)
Error in `colnames<-`(`*tmp*`, value = c("CanovesSamalus", "Llinars",  : 
  length of 'dimnames' [2] not equal to array extent

何が問題なのでしょうか?

> vols[1:3,]
    data CanovesSamalus Llinars LlinarsCanoves Mirador Santacana StAntoniVilamajor   N1    N3   N4   N7   N9
1 2011-01-01             NA  172.66       3779.832 453.096  4996.344           1830.09 9898 9898 9898 9898 9898
2 2011-01-02             NA  172.66       3809.036 494.977  4699.088           1712.43 9898 9898 9898 9898 9898
3 2011-01-03             NA  172.66       3134.215 517.979  4319.374           1716.67 9898 9898 9898 9898 9898

> str(vols)
List of 12
 $ data             : Date[1:365], format: "2011-01-01" "2011-01-02" "2011-01-03" "2011-01-04" ...
 $ CanovesSamalus   : num [1:365] NA NA NA NA NA NA NA NA NA NA ...
 $ Llinars          : num [1:365] 173 173 173 172 172 ...
 $ LlinarsCanoves   : num [1:365] 3780 3809 3134 3221 3113 ...
 $ Mirador          : num [1:365] 453 495 518 516 495 ...
 $ Santacana        : num [1:365] 4996 4699 4319 4276 4179 ...
 $ StAntoniVilamajor: num [1:365] 1830 1712 1717 1671 1733 ...
 $ N1               : num [1:365] 9898 9898 9898 9898 9898 ...
 $ N3               : num [1:365] 9898 9898 9898 9898 9898 ...
 $ N4               : num [1:365] 9898 9898 9898 9898 9898 ...
 $ N7               : num [1:365] 9898 9898 9898 9898 9898 ...
 $ N9               : num [1:365] 9898 9898 9898 9898 9898 ...
 - attr(*, "row.names")= int [1:365] 1 2 3 4 5 6 7 8 9 10 ...
 - attr(*, "idvars")= chr "data"
 - attr(*, "rdimnames")=List of 2
  ..$ :'data.frame':    365 obs. of  1 variable:
  .. ..$ data: Date[1:365], format: "2011-01-01" "2011-01-02" "2011-01-03" "2011-01-04" ...
  ..$ :'data.frame':    6 obs. of  1 variable:
  .. ..$ node: Factor w/ 6 levels "CanovesSamalus",..: 1 2 3 4 5 6

とします。

> flow[1:3,]
    data N8FT01001 N9FT00201 N9FT00401 N9FT00402 N9FT00403 N9FT00801 N9FT01001 N9FT01002 N9FT01101 N9FT01201 N9FT01202
1 2011-01-01      7123         0      2113      3105       172       791      3909      2489      1188      1182      1135
2 2011-01-02      4197         0      1984      1887       152       801      2255      2449      1227      1221      1090
3 2011-01-03      3685         0      2221      1664        27       769      1969      2516      1512      1509      1400
N9FT01301 N9FT01401
1       118       119
2       114       115
3        77        78


> str(flow)
List of 14
 $ data     : Date[1:365], format: "2011-01-01" "2011-01-02" "2011-01-03" "2011-01-04" ...
 $ N8FT01001: int [1:365] 7123 4197 3685 4616 4534 6024 3118 5977 5455 2845 ...
 $ N9FT00201: int [1:365] 0 0 0 0 0 0 0 0 0 0 ...
 $ N9FT00401: int [1:365] 2113 1984 2221 2010 1994 1776 2049 1953 2068 2028 ...
 $ N9FT00402: int [1:365] 3105 1887 1664 2066 2004 2741 1395 2645 2563 1064 ...
 $ N9FT00403: int [1:365] 172 152 27 4 14 180 14 166 155 5 ...
 $ N9FT00801: int [1:365] 791 801 769 789 688 663 730 732 767 694 ...
 $ N9FT01001: int [1:365] 3909 2255 1969 2487 2467 3198 1681 3249 2812 1740 ...
 $ N9FT01002: int [1:365] 2489 2449 2516 2551 2582 2476 2513 2582 2584 2602 ...
 $ N9FT01101: int [1:365] 1188 1227 1512 1236 1317 1006 1449 1171 1327 1345 ...
 $ N9FT01201: int [1:365] 1182 1221 1509 1232 1313 1002 1445 1167 1321 1341 ...
 $ N9FT01202: int [1:365] 1135 1090 1400 1112 1236 922 1283 1048 1218 1137 ...
 $ N9FT01301: int [1:365] 118 114 77 92 102 102 137 115 133 174 ...
 $ N9FT01401: int [1:365] 119 115 78 93 103 103 138 116 134 175 ...
 - attr(*, "row.names")= int [1:365] 1 2 3 4 5 6 7 8 9 10 ...
 - attr(*, "idvars")= chr "data"
 - attr(*, "rdimnames")=List of 2
  ..$ :'data.frame':    365 obs. of  1 variable:
  .. ..$ data: Date[1:365], format: "2011-01-01" "2011-01-02" "2011-01-03" "2011-01-04" ...
  ..$ :'data.frame':    13 obs. of  1 variable:
  .. ..$ TAG: Factor w/ 13 levels "N8FT01001","N9FT00201",..: 1 2 3 4 5 6 7 8 9 10 ...

解決方法は?

そのdata.frameをどのように作成したのかが不明です。からの str の出力は普通の data.frames ではなく、他の種類のリストのように見えます。使用方法 as.matrix(as.data.frame(vols)) .