Converting dataframe into time series

Hello everyone, I'm very new to R and I'm having a bit of difficulty with my data. I have 11 Economic variables a single country over a 21 year time span (from 1992 to 2013). I'm reading the data from csv file and then trying to define it as time series data using the ts() function. For some reason my figures are completely converted when I do so and I can't seem to figure out why. Below is a sample of my data before time series and after. If anyone can shed some light into this, I would appreciate it.

Thank you

countrydata<-read.csv("C:....Grad.csv", header = T)
model<-countrydata[,c(1:10)]
names(model)<-c("Year","fdi","rates","inflation","exports","imports","rd","electric","ggdp","gdp")
regmodel<-ts(model,start = 1992,end = 2013,frequency = 1)


#before time series
print(model)
   ï..Year   FDI  Rates Inflation    Exp    Imp    RD  Elec  GDP.G
1     1991 0.03   3.62     13.75   8.35   8.35     .  0.01   1.06 
2     1992 0.09   9.13      8.97   8.69   9.42     .  0.03   5.48 
3     1993 0.19   5.81      9.86   9.66   9.65     .  0.03   4.75 
4     1994 0.29   4.34      9.98   9.72  10.01     .  0.05   6.66 
5     1995 0.58   5.86      9.06  10.66  11.82     .  0.13   7.57 
6     1996 0.61   7.79      7.58  10.21  11.35  0.63  0.21   7.55 

#After time series
> print(regmodel)
Time Series:
Start = 1992 
End = 2013 
Frequency = 1 

     Year fdi rates inflation exports imports rd electric ggdp gdp
1992 1991   2     6         2      21      22  1        2    2   8
1993 1992   3    23        20      22      23  1        3   12  10
1994 1993   4    13        23      23      24  1        3    9   9
1995 1994   5     8        24      24       2  1        4   15  11
1996 1995   7    14        22       4       5  1        5   17  12
1997 1996   8    19        16       2       3  2        6   16  13






Welcome to the community!

What I find most surprising in your example that even after changing the column names explicitly, it's not changed while you print model. But they are there while you print regmodel. This shouldn't happen ever. Can you reproduce it? If you can, please provide a minimal REPRoducible EXample. Please include your sessionInfo() too.

The next surprising thing is of course the change of values from model and regmodel. If you are able to reproduce even this, include this part also in the reprex.

Finally, are you sure that you are showing us the correct dataset? You created model with 10 columns, but while printing, it has 9 columns!! And a 10^{th} column suddenly comes out of nowhere in regmodel. Also, the year starts from 1991 and ends at 1996, and you set it from 1992 to 2013.

What I can say that the following code works as I would expect it to on my system. I don't know what is ".", and hence considered that as a missing value.

dataset <- read.table(text = "ï..Year   FDI Rates   Inflation   Exp Imp RD  Elec    GDP.G
1991    0.03    3.62    13.75   8.35    8.35    .   0.01    1.06
1992    0.09    9.13    8.97    8.69    9.42    .   0.03    5.48
1993    0.19    5.81    9.86    9.66    9.65    .   0.03    4.75
1994    0.29    4.34    9.98    9.72    10.01   .   0.05    6.66
1995    0.58    5.86    9.06    10.66   11.82   .   0.13    7.57
1996    0.61    7.79    7.58    10.21   11.35   0.63    0.21    7.55",
                      header = TRUE,
                      na.strings = ".")
names(x = dataset) <- c("Year", "fdi", "rates", "inflation", "exports", "imports", "rd", "electric", "ggdp")
dataset
#>   Year  fdi rates inflation exports imports   rd electric ggdp
#> 1 1991 0.03  3.62     13.75    8.35    8.35   NA     0.01 1.06
#> 2 1992 0.09  9.13      8.97    8.69    9.42   NA     0.03 5.48
#> 3 1993 0.19  5.81      9.86    9.66    9.65   NA     0.03 4.75
#> 4 1994 0.29  4.34      9.98    9.72   10.01   NA     0.05 6.66
#> 5 1995 0.58  5.86      9.06   10.66   11.82   NA     0.13 7.57
#> 6 1996 0.61  7.79      7.58   10.21   11.35 0.63     0.21 7.55

dataset_as_time_series <- ts(data = dataset[-1],
                             start = 1991,
                             end = 1996,
                             frequency = 1)
dataset_as_time_series
#> Time Series:
#> Start = 1991 
#> End = 1996 
#> Frequency = 1 
#>       fdi rates inflation exports imports   rd electric ggdp
#> 1991 0.03  3.62     13.75    8.35    8.35   NA     0.01 1.06
#> 1992 0.09  9.13      8.97    8.69    9.42   NA     0.03 5.48
#> 1993 0.19  5.81      9.86    9.66    9.65   NA     0.03 4.75
#> 1994 0.29  4.34      9.98    9.72   10.01   NA     0.05 6.66
#> 1995 0.58  5.86      9.06   10.66   11.82   NA     0.13 7.57
#> 1996 0.61  7.79      7.58   10.21   11.35 0.63     0.21 7.55

Created on 2019-08-28 by the reprex package (v0.3.0)

So I managed to reprex my data. I hope I did this correctly.

econdata<-read.csv("C:/Users/Hany Osman/Desktop/R/Grad.csv", header = T)
model<-econdata[,c(1:10)]
names(model)<-c("Year","fdi","rates","inflation","exports","imports","rd","electric","ggdp","gdp")
regmodel<-ts(model,start = 1992,end = 2013,frequency = 1)
install.packages("wrapr")
#> Installing package into 'C:/Users/Hany Osman/Documents/R/win-library/3.6'
#> (as 'lib' is unspecified)
#> package 'wrapr' successfully unpacked and MD5 sums checked
#> 
#> The downloaded binary packages are in
#>  C:\Users\Hany Osman\AppData\Local\Temp\RtmpmOFNF1\downloaded_packages
require(wrapr)
#> Loading required package: wrapr
cat(draw_frame(model))
#> model <- wrapr::build_frame(
#>    "Year"  , "fdi"  , "rates" , "inflation", "exports", "imports", "rd"   , "electric", "ggdp"  , "gdp"               |
#>      1991L , "0.03 ", "3.62 " , "13.75 "   , "8.35 "  , "8.35 "  , " . "  , "0.01 "   , "1.06 " , "275000000000.00 "  |
#>      1992L , "0.09 ", "9.13 " , "8.97 "    , "8.69 "  , "9.42 "  , " . "  , "0.03 "   , "5.48 " , "293000000000.00 "  |
#>      1993L , "0.19 ", "5.81 " , "9.86 "    , "9.66 "  , "9.65 "  , " . "  , "0.03 "   , "4.75 " , "284000000000.00 "  |
#>      1994L , "0.29 ", "4.34 " , "9.98 "    , "9.72 "  , "10.01 " , " . "  , "0.05 "   , "6.66 " , "333000000000.00 "  |
#>      1995L , "0.58 ", "5.86 " , "9.06 "    , "10.66 " , "11.82 " , " . "  , "0.13 "   , "7.57 " , "367000000000.00 "  |
#>      1996L , "0.61 ", "7.79 " , "7.58 "    , "10.21 " , "11.35 " , "0.63 ", "0.21 "   , "7.55 " , "400000000000.00 "  |
#>      1997L , "0.85 ", "6.91 " , "6.48 "    , "10.51 " , "11.72 " , "0.67 ", "0.22 "   , "4.05 " , "423000000000.00 "  |
#>      1998L , "0.61 ", "5.12 " , "8.01 "    , "10.83 " , "12.46 " , "0.69 ", "0.23 "   , "6.18 " , "429000000000.00 "  |
#>      1999L , "0.46 ", "9.19 " , "3.07 "    , "11.25 " , "13.13 " , "0.71 ", "0.46 "   , "8.85 " , "467000000000.00 "  |
#>      2000L , "0.75 ", "8.34 " , "3.65 "    , "12.77 " , "13.66 " , "0.74 ", "0.54 "   , "3.84 " , "477000000000.00 "  |
#>      2001L , "1.11 ", "8.59 " , "3.22 "    , "12.34 " , "13.20 " , "0.72 ", "0.70 "   , "4.82 " , "494000000000.00 "  |
#>      2002L , "1.07 ", "7.91 " , "3.72 "    , "14.02 " , "14.98 " , "0.71 ", "0.76 "   , "3.80 " , "524000000000.00 "  |
#>      2003L , "0.70 ", "7.31 " , "3.87 "    , "14.69 " , "15.37 " , "0.71 ", "0.86 "   , "7.86 " , "618000000000.00 "  |
#>      2004L , "0.80 ", "4.91 " , "5.73 "    , "17.55 " , "19.31 " , "0.74 ", "0.96 "   , "7.92 " , "722000000000.00 "  |
#>      2005L , "0.87 ", "6.25 " , "4.24 "    , "19.28 " , "22.03 " , "0.81 ", "1.17 "   , "9.28 " , "834000000000.00 "  |
#>      2006L , "2.11 ", "4.48 " , "6.42 "    , "21.07 " , "24.23 " , "0.80 ", "1.56 "   , "9.26 " , "949000000000.00 "  |
#>      2007L , "2.04 ", "6.87 " , "5.76 "    , "20.43 " , "24.45 " , "0.79 ", "3.24 "   , "9.80 " , "1240000000000.00 " |
#>      2008L , "3.55 ", "4.28 " , "8.66 "    , "23.60 " , "28.67 " , "0.84 ", "3.47 "   , "3.89 " , "1220000000000.00 " |
#>      2009L , "2.61 ", "5.77 " , "6.06 "    , "20.05 " , "25.43 " , "0.82 ", "4.22 "   , "8.48 " , "1370000000000.00 " |
#>      2010L , "1.60 ", "(0.60)", "8.98 "    , "21.97 " , "26.34 " , "0.80 ", "4.44 "   , "10.26 ", "1710000000000.00 " |
#>      2011L , "1.94 ", "1.50 " , "8.54 "    , "23.87 " , "30.21 " , "0.81 ", "5.00 "   , "6.64 " , "1880000000000.00 " |
#>      2012L , "1.29 ", "3.20 " , "7.17 "    , "24.00 " , "30.74 " , " . "  , " . "     , "4.74 " , "1860000000000.00 " |
#>      2013L , "1.50 ", "3.16 " , "6.91 "    , "24.82 " , "28.41 " , " . "  , " . "     , "5.02 " , "1880000000000.00 " |
#>      2014L , " .. " , " .. "  , " .. "     , " .. "   , " .. "   , " . "  , " . "     , " .. "  , " .. "              )

This is not reproducible since we don't have access to your local files, please share your sample data on a copy/paste friendly format, see this nice blog post by Mara that explains how to do it

Thanks andresrcs. Following the link you provided, if I did this correctly, here is my sample data:

figures<-tibble::tribble(
           ~Year, ~FDI, ~Rates, ~Inflation,  ~Exp,
            1991, 0.03,   3.62,      13.75,  8.35,
            1992, 0.09,   9.13,       8.97,  8.69,
            1993, 0.19,   5.81,       9.86,  9.66,
            1994, 0.29,   4.34,       9.98,  9.72,
            1995, 0.58,   5.86,       9.06, 10.66,
            1996, 0.61,   7.79,       7.58, 10.21,
            1997, 0.85,   6.91,       6.48, 10.51
           )
head(figures)
#> # A tibble: 6 x 5
#>    Year   FDI Rates Inflation   Exp
#>   <dbl> <dbl> <dbl>     <dbl> <dbl>
#> 1  1991 0.03   3.62     13.8   8.35
#> 2  1992 0.09   9.13      8.97  8.69
#> 3  1993 0.19   5.81      9.86  9.66
#> 4  1994 0.290  4.34      9.98  9.72
#> 5  1995 0.580  5.86      9.06 10.7 
#> 6  1996 0.61   7.79      7.58 10.2

Sorry but your issue is not reproducible with this sample data (see reprex bellow), maybe if you try using dput() to share sample data showing the structure of your actual data, it is as easy as running dput(countrydata) and posting the result.

figures <- tibble::tribble(
    ~Year, ~FDI, ~Rates, ~Inflation,  ~Exp,
    1991, 0.03,   3.62,      13.75,  8.35,
    1992, 0.09,   9.13,       8.97,  8.69,
    1993, 0.19,   5.81,       9.86,  9.66,
    1994, 0.29,   4.34,       9.98,  9.72,
    1995, 0.58,   5.86,       9.06, 10.66,
    1996, 0.61,   7.79,       7.58, 10.21,
    1997, 0.85,   6.91,       6.48, 10.51
)
names(figures)<-c("Year","fdi","rates","inflation","exports")
regmodel <- ts(figures,start = 1991,end = 1997,frequency = 1)
figures
#> # A tibble: 7 x 5
#>    Year   fdi rates inflation exports
#>   <dbl> <dbl> <dbl>     <dbl>   <dbl>
#> 1  1991 0.03   3.62     13.8     8.35
#> 2  1992 0.09   9.13      8.97    8.69
#> 3  1993 0.19   5.81      9.86    9.66
#> 4  1994 0.290  4.34      9.98    9.72
#> 5  1995 0.580  5.86      9.06   10.7 
#> 6  1996 0.61   7.79      7.58   10.2 
#> 7  1997 0.85   6.91      6.48   10.5
regmodel
#> Time Series:
#> Start = 1991 
#> End = 1997 
#> Frequency = 1 
#>      Year  fdi rates inflation exports
#> 1991 1991 0.03  3.62     13.75    8.35
#> 1992 1992 0.09  9.13      8.97    8.69
#> 1993 1993 0.19  5.81      9.86    9.66
#> 1994 1994 0.29  4.34      9.98    9.72
#> 1995 1995 0.58  5.86      9.06   10.66
#> 1996 1996 0.61  7.79      7.58   10.21
#> 1997 1997 0.85  6.91      6.48   10.51

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