I would like to set an existing variable ( year
) at a time-series variable for use in regressions in R.
I have a dataset (in data-frame format ) of country-level socioeconomic information from 1999 to 2013. Not every country has observations for every year. Some fill the entire range (1999-2013). Others do not.
The data look like this:
data:
year country economy poverty
2011 Algeria 1.0814223 1.3310658
2012 Algeria 0.4172840 0.6206897
2013 Algeria 0.5652174 0.9222222
1999 Morocco 1.0814223 1.3310658
2000 Morocco 1.3310658 1.1468254
2001 Morocco 0.6206897 1.2805829
2002 Morocco 0.9222222 1.3310658
2003 Morocco 1.6500000 0.7612833
2004 Morocco 0.9126984 0.9768519
2005 Morocco 1.1388889 1.3310658
2006 Morocco 1.0500000 0.8011516
2007 Morocco 1.0458075 0.5761905
2008 Morocco 1.5017544 0.6222222
2009 Morocco 1.1666667 1.3310658
2010 Morocco 1.0527660 1.1468254
2011 Morocco 1.3611111 0.5761905
2012 Morocco 1.1666667 0.5761905
2013 Morocco 1.4137931 0.5761905
How can I tell R to treat these data as time-series? I have tried the following:
Setting the entire dataframe as a time series:
ts.data <- as.ts(data, start = 1999, end = 2013)
Setting a time-series variable within the dataset:
data$timeseries <- ts(data$year, start = 1999, end = 2013, frequency = 1)
Unfortunately, neither works.
I'm sure that this is a dumb question, and I apologize. Thank you in advance for your help!
Assuming you really do want to create a time series from this as stated in the question:
library(zoo)
z <- read.zoo(data, split = "country")
giving this "zoo"
class object (continued after output):
> z
economy.Algeria poverty.Algeria economy.Morocco poverty.Morocco
1999 NA NA 1.0814223 1.3310658
2000 NA NA 1.3310658 1.1468254
2001 NA NA 0.6206897 1.2805829
2002 NA NA 0.9222222 1.3310658
2003 NA NA 1.6500000 0.7612833
2004 NA NA 0.9126984 0.9768519
2005 NA NA 1.1388889 1.3310658
2006 NA NA 1.0500000 0.8011516
2007 NA NA 1.0458075 0.5761905
2008 NA NA 1.5017544 0.6222222
2009 NA NA 1.1666667 1.3310658
2010 NA NA 1.0527660 1.1468254
2011 1.0814223 1.3310658 1.3611111 0.5761905
2012 0.4172840 0.6206897 1.1666667 0.5761905
2013 0.5652174 0.9222222 1.4137931 0.5761905
or to convert to "ts"
class:
tt <- as.ts(z)
Note: The input data
in reproducible form is:
Lines <- "year country economy poverty
2011 Algeria 1.0814223 1.3310658
2012 Algeria 0.4172840 0.6206897
2013 Algeria 0.5652174 0.9222222
1999 Morocco 1.0814223 1.3310658
2000 Morocco 1.3310658 1.1468254
2001 Morocco 0.6206897 1.2805829
2002 Morocco 0.9222222 1.3310658
2003 Morocco 1.6500000 0.7612833
2004 Morocco 0.9126984 0.9768519
2005 Morocco 1.1388889 1.3310658
2006 Morocco 1.0500000 0.8011516
2007 Morocco 1.0458075 0.5761905
2008 Morocco 1.5017544 0.6222222
2009 Morocco 1.1666667 1.3310658
2010 Morocco 1.0527660 1.1468254
2011 Morocco 1.3611111 0.5761905
2012 Morocco 1.1666667 0.5761905
2013 Morocco 1.4137931 0.5761905"
data <- read.table(text = Lines, header = TRUE)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.