将R中的每月数据转换为每季度数据

时间:2019-08-02 22:20:20

标签: r aggregate

我创建了一个正在运行的时间序列

debit<-ts(t(data1[7,-1]),start=c(2006,1),freq=12)

然后,尝试使用aggregate()将freq = 12转换为freq = 4

debitq<-aggregate(debit,nfrequency=4)

我收到消息

"Error in FUN(newX[, i], ...) : invalid 'type' (character) of argument"

我尝试将sum作为FUN包含在内,但不起作用

ts:

           Jan       Feb       Mar       Apr       May       Jun       Jul       Aug       Sep       Oct       Nov       Dec
2006  1151.20   1166.29   1176.28   1138.82   1151.16   1156.34   1154.89   1174.97   1201.12   1204.47   1225.78   1236.95 
2007  1228.42   1260.39   1278.27   1285.94   1299.29   1325.34   1289.39   1312.27   1315.92   1307.14   1329.95   1333.75 
2008  1311.34   1345.38   1356.27   1318.32   1337.18   1343.40   1297.92   1319.52   1335.10   1345.36   1374.38   1397.34 
2009  1352.24   1381.00   1398.24   1384.03   1388.31   1434.60   1457.09   1509.95   1488.93   1472.43   1491.84   1497.39 
2010  1457.66   1494.97   1495.12   1585.07   1614.42   1612.41   1601.38   1618.11   1626.17   1644.93   1666.35   1694.04 
2011  1628.99   1671.78   1695.05   1734.68   1746.29   1805.43   1734.45   1768.39   1808.74   1806.56   1833.54   1866.35 
2012  1801.11   1836.04   1855.93   1880.44   1922.03   1970.70   1876.81   1867.30   1904.96   1943.84   1965.22   2007.98 
2013  1925.81   1951.58   1940.53   1940.31   1935.20   1985.58   1957.00   1991.67   1988.86   2022.52   2069.44   2122.81 
2014  2046.30   2067.28   2080.61   2052.56   2122.92   2202.97   2173.18   2169.54   2183.60   2155.37   2208.96   2295.90 
2015  2247.88   2329.62   2441.20   2451.47   2496.23   2583.69   2603.96   2686.29   2734.63   2646.48   2716.92   2793.01 
2016  2749.88   2819.47   2886.70   2799.79   2878.87   2958.64   2956.51   2955.19   3046.91   3032.89   3092.66   3112.94 
2017  3053.35   3134.67   3234.14   3244.51   3253.03   3357.65   3341.38   3404.00   3430.83   3438.48   3493.38   3559.27 
2018  3528.31   3582.15   3636.33   3658.51   3716.71   3754.10   3748.84   3785.66   3779.48   3763.04   3826.74   3877.06 
2019  3808.26   3873.53   3917.95   3878.69   3890.85   3977.99                                                             

dput():

    V163 = structure(c(1L, 1L, 26L, 1L, 27L, 1L, 15L, 1L, 14L, 
    1L, 4L, 24L, 25L, 23L, 1L, 20L, 13L, 3L, 17L, 1L, 1L, 1L, 
    11L, 10L, 8L, 19L, 7L, 1L, 1L, 9L, 12L, 6L, 1L, 1L, 5L, 18L, 
    22L, 21L, 16L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", " 0,00 ", " 1,32 ", 
    " 1.497,49 ", " 1.826,87 ", " 10,06 ", " 10,80 ", " 122,91 ", 
    " 13,59 ", " 138,09 ", " 151,68 ", " 3,53 ", " 3,96 ", " 3.826,31 ", 
    " 3.977,99 ", " 309,25 ", " 36,67 ", " 399,70 ", " 4,38 ", 
    " 410,42 ", " 455,16 ", " 662,76 ", " 84,00 ", " 843,91 ", 
    " 948,54 ", "($)", "Jun/19"), class = "factor")), class = "data.frame", row.names = c(NA, 
-56L))

traceback():

> traceback()
2: apply(array(c(x[1:nend, ]), dim = c(len, nend/len, ncol(x))), 
       MARGIN = c(2L, 3L), FUN = FUN, ...)
1: aggregate.ts(debit, nfrequency = 4)

sapply(借方,类):

> sapply(debit, class)
   7 
"ts" 

1 个答案:

答案 0 :(得分:0)

据我所知,我的最佳猜测是您使用read.csv中的read.tableutils加载数据。而不是NA,您要加载到R中的原始文件包含空字符串“”(dput中因子的第一级),这导致整个列被视为字符并在加载时变为因子。 在这种情况下,您应该这样做:

data <- read.csv(<YOURFILE>, <YOURARGUMENTS>, colClasses = "character",
 stringsAsFactors = FALSE) # or read.table(... whichever you are using
data[[1]] <- factor(data[[1]])
for(i in 2:ncol(data)) data[[i]] <- as.numeric(data[[i]])