Hi All, I have a sample of data set show as below. tdat <- read.table(textConnection("stat year Y AL 2003 25 AL 2003 13 AL 2004 21 AL 2006 20 AL 2007 12 AL 2009 16 AL 2010 15 FL 2006 63 FL 2007 14 FL 2007 25 FL 2009 64 FL 2009 47 FL 2010 48 NY 2003 50 NY 2004 51 NY 2006 57 NY 2007 62 NY 2007 36 NY 2009 87 NY 2009 96 NY 2010 91 NY 2010 59 NY 2010 80"),header = TRUE,stringsAsFactors=FALSE) There are three states, I wan tto select states taht do ahve records in all year. Example, xtabs(Y~stat+year, tdat) This gave me the following stat 2003 2004 2006 2007 2009 2010 AL 38 21 20 12 16 15 FL 0 0 63 39 111 48 NY 50 51 57 98 183 230 Fl state does not have recrods in all year and I wan to exclude from this and I want teh result as follow stat 2003 2004 2006 2007 2009 2010 AL 38 21 20 12 16 15 NY 50 51 57 98 183 230 The other thing, how do I get teh counts state by year? Desired result, 2003 2004 2006 2007 2009 2010 AL 2 1 1 1 1 1 NY 1 1 1 2 2 3 Thank you [[alternative HTML version deleted]]
Hi Val, This may help: tdat$allpresent<-FALSE for(state in allstates) tdat$allpresent[tdat$stat == state]<- all(allyears %in% tdat$year[tdat$stat==state]) tdat2<-tdat[tdat$allpresent,] xtabs(Y~stat+year,tdat2) table(tdat2$stat,tdat2$year) Jim On Fri, May 18, 2018 at 10:48 AM, Val <valkremk at gmail.com> wrote:> Hi All, > > I have a sample of data set show as below. > tdat <- read.table(textConnection("stat year Y > AL 2003 25 > AL 2003 13 > AL 2004 21 > AL 2006 20 > AL 2007 12 > AL 2009 16 > AL 2010 15 > FL 2006 63 > FL 2007 14 > FL 2007 25 > FL 2009 64 > FL 2009 47 > FL 2010 48 > NY 2003 50 > NY 2004 51 > NY 2006 57 > NY 2007 62 > NY 2007 36 > NY 2009 87 > NY 2009 96 > NY 2010 91 > NY 2010 59 > NY 2010 80"),header = TRUE,stringsAsFactors=FALSE) > > There are three states, I wan tto select states taht do ahve records in all > year. > Example, > xtabs(Y~stat+year, tdat) > This gave me the following > > stat 2003 2004 2006 2007 2009 2010 > AL 38 21 20 12 16 15 > FL 0 0 63 39 111 48 > NY 50 51 57 98 183 230 > > Fl state does not have recrods in all year and I wan to exclude from this > and I want teh result as follow > > stat 2003 2004 2006 2007 2009 2010 > AL 38 21 20 12 16 15 > NY 50 51 57 98 183 230 > > The other thing, how do I get teh counts state by year? > > Desired result, > > 2003 2004 2006 2007 2009 2010 > AL 2 1 1 1 1 1 > NY 1 1 1 2 2 3 > > Thank you > > [[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.
... and similar to Jim's suggestion but perhaps slightly simpler (or not!):> cross <- xtabs( Y ~ stat + year, data = tdat) > keep <- apply(cross, 1, all) > keep <- names(keep)[keep] > cross[keep,]year stat 2003 2004 2006 2007 2009 2010 AL 38 21 20 12 16 15 NY 50 51 57 98 183 230> ## for counts just do: > xtabs( ~ stat + year, data = tdat[tdat$stat %in% keep, ])year stat 2003 2004 2006 2007 2009 2010 AL 2 1 1 1 1 1 NY 1 1 1 2 2 3 Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Thu, May 17, 2018 at 5:48 PM, Val <valkremk at gmail.com> wrote:> Hi All, > > I have a sample of data set show as below. > tdat <- read.table(textConnection("stat year Y > AL 2003 25 > AL 2003 13 > AL 2004 21 > AL 2006 20 > AL 2007 12 > AL 2009 16 > AL 2010 15 > FL 2006 63 > FL 2007 14 > FL 2007 25 > FL 2009 64 > FL 2009 47 > FL 2010 48 > NY 2003 50 > NY 2004 51 > NY 2006 57 > NY 2007 62 > NY 2007 36 > NY 2009 87 > NY 2009 96 > NY 2010 91 > NY 2010 59 > NY 2010 80"),header = TRUE,stringsAsFactors=FALSE) > > There are three states, I wan tto select states taht do ahve records in all > year. > Example, > xtabs(Y~stat+year, tdat) > This gave me the following > > stat 2003 2004 2006 2007 2009 2010 > AL 38 21 20 12 16 15 > FL 0 0 63 39 111 48 > NY 50 51 57 98 183 230 > > Fl state does not have recrods in all year and I wan to exclude from this > and I want teh result as follow > > stat 2003 2004 2006 2007 2009 2010 > AL 38 21 20 12 16 15 > NY 50 51 57 98 183 230 > > The other thing, how do I get teh counts state by year? > > Desired result, > > 2003 2004 2006 2007 2009 2010 > AL 2 1 1 1 1 1 > NY 1 1 1 2 2 3 > > Thank you > > [[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. >[[alternative HTML version deleted]]
Thank you Bert and Jim, Jim, FYI , I have an error message generated as Error in allstates : object 'allstates' not found Bert, it is working. However, If I want to chose to include only mos years example, 2003,2004,2007 and continue the analysis as before. Where should I define the years to get as follow. 2003 2004 2007 AL 2 1 1 NY 1 1 2 Thank you again. On Thu, May 17, 2018 at 8:48 PM, Bert Gunter <bgunter.4567 at gmail.com> wrote:> ... and similar to Jim's suggestion but perhaps slightly simpler (or not!): > > > cross <- xtabs( Y ~ stat + year, data = tdat) > > keep <- apply(cross, 1, all) > > keep <- names(keep)[keep] > > cross[keep,] > year > stat 2003 2004 2006 2007 2009 2010 > AL 38 21 20 12 16 15 > NY 50 51 57 98 183 230 > > > > > ## for counts just do: > > xtabs( ~ stat + year, data = tdat[tdat$stat %in% keep, ]) > year > stat 2003 2004 2006 2007 2009 2010 > AL 2 1 1 1 1 1 > NY 1 1 1 2 2 3 > > Cheers, > Bert > > Bert Gunter > > "The trouble with having an open mind is that people keep coming along and > sticking things into it." > -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) > > On Thu, May 17, 2018 at 5:48 PM, Val <valkremk at gmail.com> wrote: > >> Hi All, >> >> I have a sample of data set show as below. >> tdat <- read.table(textConnection("stat year Y >> AL 2003 25 >> AL 2003 13 >> AL 2004 21 >> AL 2006 20 >> AL 2007 12 >> AL 2009 16 >> AL 2010 15 >> FL 2006 63 >> FL 2007 14 >> FL 2007 25 >> FL 2009 64 >> FL 2009 47 >> FL 2010 48 >> NY 2003 50 >> NY 2004 51 >> NY 2006 57 >> NY 2007 62 >> NY 2007 36 >> NY 2009 87 >> NY 2009 96 >> <https://maps.google.com/?q=2009%C2%A0+%C2%A0+96+%0D%0ANY&entry=gmail&source=g> >> NY 2010 91 >> NY 2010 59 >> NY 2010 80"),header = TRUE,stringsAsFactors=FALSE) >> >> There are three states, I wan tto select states taht do ahve records in >> all >> year. >> Example, >> xtabs(Y~stat+year, tdat) >> This gave me the following >> >> stat 2003 2004 2006 2007 2009 2010 >> AL 38 21 20 12 16 15 >> FL 0 0 63 39 111 48 >> NY 50 51 57 98 183 230 >> >> Fl state does not have recrods in all year and I wan to exclude from this >> and I want teh result as follow >> >> stat 2003 2004 2006 2007 2009 2010 >> AL 38 21 20 12 16 15 >> NY 50 51 57 98 183 230 >> >> The other thing, how do I get teh counts state by year? >> >> Desired result, >> >> 2003 2004 2006 2007 2009 2010 >> AL 2 1 1 1 1 1 >> NY 1 1 1 2 2 3 >> >> Thank you >> >> [[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/posti >> ng-guide.html >> and provide commented, minimal, self-contained, reproducible code. >> > >[[alternative HTML version deleted]]