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 <valkr...@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@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. ______________________________________________ R-help@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.