How to convert build-in datasets to table?

Hello community.

I want to use "UKgas" in package datasets as table but I can't.

How to convert these package datasets to table like csv we usually use?

Thank you.

UKgas is a time series, which looks like a data frame but isn't. Converting a time series object to a data frame is possible and then it can be saved as a csv., To reuse it as a time series it would have to be converted back again. To make a rectangular object that looks like UKgas, this works

years <- 1960:1986
d <- cbind(years,as.data.frame(matrix(UKgas,nrow = 27,ncol = 4)))
colnames(d) <- c("years","Qtr1","Qtr2","Qtr3","Qtr4")
d
#>    years  Qtr1  Qtr2  Qtr3   Qtr4
#> 1   1960 160.1 136.1 158.5  414.6
#> 2   1961 129.7 204.9 355.4  217.7
#> 3   1962  84.8 176.1 449.9  670.8
#> 4   1963 120.1 112.1 286.6  848.5
#> 5   1964 160.1 140.9 179.3  437.0
#> 6   1965 124.9 227.3 403.4  209.7
#> 7   1966  84.8 195.3 491.5  701.2
#> 8   1967 116.9 115.3 321.8  925.3
#> 9   1968 169.7 142.5 177.7  443.4
#> 10  1969 140.9 244.9 409.8  214.5
#> 11  1970  89.7 214.5 593.9  683.6
#> 12  1971 123.3 118.5 329.8  917.3
#> 13  1972 187.3 153.7 176.1  515.5
#> 14  1973 144.1 244.9 483.5  224.1
#> 15  1974  92.9 216.1 584.3  694.8
#> 16  1975 120.1 188.9 395.4  989.4
#> 17  1976 176.1 142.5 187.3  477.1
#> 18  1977 147.3 301.0 485.1  233.7
#> 19  1978  89.7 196.9 669.2  730.0
#> 20  1979 123.3 136.1 421.0 1087.0
#> 21  1980 185.7 267.3 216.1  534.7
#> 22  1981 155.3 317.0 509.1  281.8
#> 23  1982  99.3 230.5 827.7  787.6
#> 24  1983 131.3 152.1 467.5 1163.9
#> 25  1984 200.1 336.2 209.7  613.1
#> 26  1985 161.7 371.4 542.7  347.4
#> 27  1986 102.5 240.1 840.5  782.8

Created on 2023-04-21 with reprex v2.0.2

but I can't recommend it without knowing more the purpose intended.

Thank you for your prompt reply.

I'm sorry not to explain enough about my purpose, but that is exactly it I was imaging!

Thanks to you, I can finish my homework!

1 Like

The first row of UKgas is 160.1 129.7 84.8 120.1, which appears as the first four elements of the first column in d above. The second row of UKgas, 160.1 124.9 84.8 116.9, becomes the next four elements in the first column of d.

With one small tweak:

years <- 1960:1986
d <- cbind(years,as.data.frame(matrix(UKgas,nrow = 27,byrow = TRUE)))
colnames(d) <- c("years","Qtr1","Qtr2","Qtr3","Qtr4")
d
#>    years   Qtr1  Qtr2  Qtr3  Qtr4
#> 1   1960  160.1 129.7  84.8 120.1
#> 2   1961  160.1 124.9  84.8 116.9
#> 3   1962  169.7 140.9  89.7 123.3
#> 4   1963  187.3 144.1  92.9 120.1
#> 5   1964  176.1 147.3  89.7 123.3
#> 6   1965  185.7 155.3  99.3 131.3
#> 7   1966  200.1 161.7 102.5 136.1
#> 8   1967  204.9 176.1 112.1 140.9
#> 9   1968  227.3 195.3 115.3 142.5
#> 10  1969  244.9 214.5 118.5 153.7
#> 11  1970  244.9 216.1 188.9 142.5
#> 12  1971  301.0 196.9 136.1 267.3
#> 13  1972  317.0 230.5 152.1 336.2
#> 14  1973  371.4 240.1 158.5 355.4
#> 15  1974  449.9 286.6 179.3 403.4
#> 16  1975  491.5 321.8 177.7 409.8
#> 17  1976  593.9 329.8 176.1 483.5
#> 18  1977  584.3 395.4 187.3 485.1
#> 19  1978  669.2 421.0 216.1 509.1
#> 20  1979  827.7 467.5 209.7 542.7
#> 21  1980  840.5 414.6 217.7 670.8
#> 22  1981  848.5 437.0 209.7 701.2
#> 23  1982  925.3 443.4 214.5 683.6
#> 24  1983  917.3 515.5 224.1 694.8
#> 25  1984  989.4 477.1 233.7 730.0
#> 26  1985 1087.0 534.7 281.8 787.6
#> 27  1986 1163.9 613.1 347.4 782.8

Created on 2023-04-22 with reprex v2.0.2

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.