Hi, I am making my way down the learning curve of R, and find it a great language with so many helpful users! Below is an example of what I'm trying to do, but can't quite figure out the right path to go down. Here's what I have: Main is a time series of data with columns Cola and Colb Cola Colb 1 1 1 2 1 1 3 1 1 4 -1 1 5 -1 -1 6 -1 -1 7 1 -1 8 -1 -1 9 -1 -1 10 -1 -1 What I would like is to subset the data into groups - rows 1-3 into one group (Cola and Colb both =1), then row 4 by itself, then rows 5-6, then row 7, then rows 8-10. Group 1 Cola Colb 1 1 1 2 1 1 3 1 1 Group 2 Cola Colb 1 -1 1 Group 3 Cola Colb 1 -1 -1 2 -1 -1 Thank you for your help! Ian [[alternative HTML version deleted]]
On Thu, Oct 4, 2012 at 3:20 PM, Ian Arvin <ian at innovativequant.com> wrote:> Hi, > > > I am making my way down the learning curve of R, and find it a great > language with so many helpful users!Up it hopefully, no?> > Below is an example of what I'm trying to do, but can't quite figure out the > right path to go down. > > > > Here's what I have: > > > > Main is a time series of data with columns Cola and Colb > > Cola Colb > > 1 1 1 > > 2 1 1 > > 3 1 1 > > 4 -1 1 > > 5 -1 -1 > > 6 -1 -1 > > 7 1 -1 > > 8 -1 -1 > > 9 -1 -1 > > 10 -1 -1 > > > > What I would like is to subset the data into groups - rows 1-3 into one > group (Cola and Colb both =1), then row 4 by itself, then rows 5-6, then row > 7, then rows 8-10. > > Group 1 > > Cola Colb > > 1 1 1 > > 2 1 1 > > 3 1 1 > > > > Group 2 > > Cola Colb > > 1 -1 1 > > > > Group 3 > > Cola Colb > > 1 -1 -1 > > 2 -1 -1 > > >Short answer is to ask why you want to do this? There are many functions which operate on the split-->apply-->combine paradigm (see the JSS article of that name for a short intro) but I find it easier to do this in conjunction with specifying what my data transformation is. To directly answer your question, you'll probably need to combine split() and interaction() in some way. Cheers, Michael
Hello, Try the following. dat <- read.table(text=" Cola Colb 1 1 1 2 1 1 3 1 1 4 -1 1 5 -1 -1 6 -1 -1 7 1 -1 8 -1 -1 9 -1 -1 10 -1 -1 ", header=TRUE) idx <- dat$Cola != dat$Colb split(dat, 2*cumsum(idx) - idx) Hope this helps, Rui Barradas Em 04-10-2012 15:20, Ian Arvin escreveu:> Hi, > > > I am making my way down the learning curve of R, and find it a great > language with so many helpful users! > > Below is an example of what I'm trying to do, but can't quite figure out the > right path to go down. > > > > Here's what I have: > > > > Main is a time series of data with columns Cola and Colb > > Cola Colb > > 1 1 1 > > 2 1 1 > > 3 1 1 > > 4 -1 1 > > 5 -1 -1 > > 6 -1 -1 > > 7 1 -1 > > 8 -1 -1 > > 9 -1 -1 > > 10 -1 -1 > > > > What I would like is to subset the data into groups - rows 1-3 into one > group (Cola and Colb both =1), then row 4 by itself, then rows 5-6, then row > 7, then rows 8-10. > > Group 1 > > Cola Colb > > 1 1 1 > > 2 1 1 > > 3 1 1 > > > > Group 2 > > Cola Colb > > 1 -1 1 > > > > Group 3 > > Cola Colb > > 1 -1 -1 > > 2 -1 -1 > > > > Thank you for your help! > > > > Ian > > > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > 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.
Hi, You can also try this: dat1 <- read.table(text=" Cola?????? Colb 1????????????? 1????????????? 1 2????????????? 1????????????? 1 3????????????? 1????????????? 1 4????????????? -1??????????? 1 5????????????? -1??????????? -1 6????????????? -1??????????? -1 7????????????? 1????????????? -1 8????????????? -1??????????? -1 9????????????? -1??????????? -1 10?????????? -1??????????? -1 ", header=TRUE) idx<- c(0,cumsum(abs(diff(dat1$Cola))))-as.integer(dat1$Cola!=dat1$Colb) ?split(dat1,idx) A.K. ----- Original Message ----- From: Ian Arvin <ian at innovativequant.com> To: r-help at r-project.org Cc: Sent: Thursday, October 4, 2012 10:20 AM Subject: [R] Subsetting a group of data Hi, I am making my way down the learning curve of R, and find it a great language with so many helpful users! Below is an example of what I'm trying to do, but can't quite figure out the right path to go down. Here's what I have: Main is a time series of data with columns Cola and Colb ? ? ? ? ? ? ? ? Cola? ? ? Colb 1? ? ? ? ? ? ? 1? ? ? ? ? ? ? 1 2? ? ? ? ? ? ? 1? ? ? ? ? ? ? 1 3? ? ? ? ? ? ? 1? ? ? ? ? ? ? 1 4? ? ? ? ? ? ? -1? ? ? ? ? ? 1 5? ? ? ? ? ? ? -1? ? ? ? ? ? -1 6? ? ? ? ? ? ? -1? ? ? ? ? ? -1 7? ? ? ? ? ? ? 1? ? ? ? ? ? ? -1 8? ? ? ? ? ? ? -1? ? ? ? ? ? -1 9? ? ? ? ? ? ? -1? ? ? ? ? ? -1 10? ? ? ? ? -1? ? ? ? ? ? -1 What I would like is to subset the data into groups -? rows 1-3 into one group (Cola and Colb both =1), then row 4 by itself, then rows 5-6, then row 7, then rows 8-10. Group 1 ? ? ? ? ? ? ? ? Cola? ? ? Colb 1? ? ? ? ? ? ? 1? ? ? ? ? ? ? 1 2? ? ? ? ? ? ? 1? ? ? ? ? ? ? 1 3? ? ? ? ? ? ? 1? ? ? ? ? ? ? 1 Group 2 ? ? ? ? ? ? ? ? Cola? ? ? Colb 1? ? ? ? ? ? ? -1? ? ? ? ? ? 1 Group 3 ? ? ? ? ? ? ? ? Cola? ? ? Colb 1? ? ? ? ? ? ? -1? ? ? ? ? ? -1 2? ? ? ? ? ? ? -1? ? ? ? ? ? -1 Thank you for your help! Ian ??? [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list 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.
HI, Another way to get the result: dat1 <- read.table(text=" Cola?????? Colb 1????????????? 1????????????? 1 2????????????? 1????????????? 1 3????????????? 1????????????? 1 4????????????? -1??????????? 1 5????????????? -1??????????? -1 6????????????? -1??????????? -1 7????????????? 1????????????? -1 8????????????? -1??????????? -1 9????????????? -1??????????? -1 10?????????? -1??????????? -1 ", header=TRUE) list1<-split(dat1,within(dat1,{new1<-as.numeric(unlist(strsplit(gsub("AAA(\\d)AA(\\d).*","111\\133\\2555",gsub("(.*)B(.*)B(.*)","\\12\\24\\3",paste(ifelse(dat1$Cola==dat1$Colb,"A","B"),collapse=""))),"")))})[,3]) list1 #$`1` ?# Cola Colb #1??? 1??? 1 #2??? 1??? 1 #3??? 1??? 1 #$`2` ?# Cola Colb #4?? -1??? 1 #$`3` ?# Cola Colb #5?? -1?? -1 #6?? -1?? -1 #$`4` ?# Cola Colb #7??? 1?? -1 #$`5` ?#? Cola Colb #8??? -1?? -1 #9??? -1?? -1 #10?? -1?? -1 A.K. ----- Original Message ----- From: Ian Arvin <ian at innovativequant.com> To: r-help at r-project.org Cc: Sent: Thursday, October 4, 2012 10:20 AM Subject: [R] Subsetting a group of data Hi, I am making my way down the learning curve of R, and find it a great language with so many helpful users! Below is an example of what I'm trying to do, but can't quite figure out the right path to go down. Here's what I have: Main is a time series of data with columns Cola and Colb ? ? ? ? ? ? ? ? Cola? ? ? Colb 1? ? ? ? ? ? ? 1? ? ? ? ? ? ? 1 2? ? ? ? ? ? ? 1? ? ? ? ? ? ? 1 3? ? ? ? ? ? ? 1? ? ? ? ? ? ? 1 4? ? ? ? ? ? ? -1? ? ? ? ? ? 1 5? ? ? ? ? ? ? -1? ? ? ? ? ? -1 6? ? ? ? ? ? ? -1? ? ? ? ? ? -1 7? ? ? ? ? ? ? 1? ? ? ? ? ? ? -1 8? ? ? ? ? ? ? -1? ? ? ? ? ? -1 9? ? ? ? ? ? ? -1? ? ? ? ? ? -1 10? ? ? ? ? -1? ? ? ? ? ? -1 What I would like is to subset the data into groups -? rows 1-3 into one group (Cola and Colb both =1), then row 4 by itself, then rows 5-6, then row 7, then rows 8-10. Group 1 ? ? ? ? ? ? ? ? Cola? ? ? Colb 1? ? ? ? ? ? ? 1? ? ? ? ? ? ? 1 2? ? ? ? ? ? ? 1? ? ? ? ? ? ? 1 3? ? ? ? ? ? ? 1? ? ? ? ? ? ? 1 Group 2 ? ? ? ? ? ? ? ? Cola? ? ? Colb 1? ? ? ? ? ? ? -1? ? ? ? ? ? 1 Group 3 ? ? ? ? ? ? ? ? Cola? ? ? Colb 1? ? ? ? ? ? ? -1? ? ? ? ? ? -1 2? ? ? ? ? ? ? -1? ? ? ? ? ? -1 Thank you for your help! Ian ??? [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list 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.