[R] Reading data with two rows of variable names using read.zoo

Nordlund, Dan (DSHS/RDA) NordlDJ at dshs.wa.gov
Wed Jul 29 02:16:09 CEST 2015


Not a guru, but this isn't that hard.  The following works with your sample data.  It shouldn't be too difficult to modify for your full file.

library(zoo)
df <- read.table('path_to_your_data', sep=';', skip=2, as.is=TRUE)
str(df)
substr(df$V1,5,5) <- '-'
df$V1 <- as.yearqtr(substr(df$V1,1,6))
df$A <- rowSums(df[,c(2,4)])
df$B <- rowSums(df[,c(3,5)])

want <- as.zoo(df[,-c(2:5)])
want

Hope this is helpful,

Dan

Daniel Nordlund, PhD
Research and Data Analysis Division
Services & Enterprise Support Administration
Washington State Department of Social and Health Services

-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of ???
Sent: Tuesday, July 28, 2015 9:42 AM
To: r-help at r-project.org
Subject: [R] Reading data with two rows of variable names using read.zoo

Dear R gurus.

I have a data file which has two rows of variable names.
And the time index has a little unusual format. I have no idea how to handle two names and awkward indexing for the quarters.

Lines <- "
Index; UK; UK; JP; JP
Index; a1; a2; a1; a2
2009 2/4;2;4;3;2
2009 3/4;5;2;1;4
2009 4/4;7;1;1;6
2010 1/4;3;3;5;2
2010 2/4;5;1;2;1
"
(a snippet from a big data containing a1, a2, ..., a10 of many countries)

I want to sum a1 and a2 for UK, JP and obtain a zoo object like this:

            A    B
2009 Q2     6    5
2009 Q3     7    5
2009 Q4     8    7
2010 Q1     6    7
2010 Q2     6    3

This looks quite challenging. Thanks for your time.

	[[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list