κΉμν
2015-Jul-28 16:41 UTC
[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]]
Nordlund, Dan (DSHS/RDA)
2015-Jul-29 00:16 UTC
[R] Reading data with two rows of variable names using read.zoo
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.
SW Kim
2015-Jul-29 06:57 UTC
[R] Reading data with two rows of variable names using read.zoo
Thanks, Dan. Your codes work fine. But I have tens of countries UK, JP, BR, US..., each of which has ten columns a1, a2, ..., a10 of data. So a little more automation is needed. I have been trying to make a list of each country's data and use sapply thing to get UK JP 2009 Q2 6 5 2009 Q3 7 5 2009 Q4 8 7 2010 Q1 6 7 2010 Q2 6 3 But for me, it was not easy as it looks... Thank you in advance! -- View this message in context: http://r.789695.n4.nabble.com/Reading-data-with-two-rows-of-variable-names-using-read-zoo-tp4710496p4710510.html Sent from the R help mailing list archive at Nabble.com.