Listers, I have created a cross-tab matrix using the following code: S <- c(1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,1,2,3,1,1,2,3,1,2,2) F <- c(1,2,3,1,2,3,1,1,1,1,2,3,1,1,3,2,2,2,1,2,3,1,1,1,1,2,3,3,1,3,1,3,1,1,2,3,1,1,3,2,3,2,1,1,1,2,3,1,1,2) table(S,F) fF <-factor(F, labels = c("Dem","Ind","Rep")) fS <-factor(S, labels = c("Dem","Ind","Rep")) PiD <- table(fS,fF) PiD <- cbind(PiD, margin.table(PiD,1)) PiD <- rbind(PiD, margin.table(PiD,2)) Now I would like to add the Column Name and Row Name "Total" to the last row and last column (where the marginal totals now hang out). When I use the command colnames(PiD[,4]) <- c("total") I get the error: Error in `colnames<-`(`*tmp*`, value = "WTF") : attempt to set colnames on object with less than two dimensions Since I am still new to R, I can't really understand the error... Can someone tell me what I am doing wrong here? Thanks [[alternative HTML version deleted]]
Hi Dmitry, Nothing is seriously wrong. You want to set the fourth column name, so take column names THEN the fourth element: colnames(PiD)[4] <- "total" Thank you for providing a clear and simple reproducible example. Sarah On Fri, Apr 15, 2011 at 9:29 AM, Dmitry Berman <ravenblur at gmail.com> wrote:> Listers, > > I have created a cross-tab matrix using the following code: > > S <- > c(1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,1,2,3,1,1,2,3,1,2,2) > F <- > c(1,2,3,1,2,3,1,1,1,1,2,3,1,1,3,2,2,2,1,2,3,1,1,1,1,2,3,3,1,3,1,3,1,1,2,3,1,1,3,2,3,2,1,1,1,2,3,1,1,2) > table(S,F) > fF <-factor(F, labels = c("Dem","Ind","Rep")) > fS <-factor(S, labels = c("Dem","Ind","Rep")) > PiD <- table(fS,fF) > PiD <- cbind(PiD, margin.table(PiD,1)) > PiD <- rbind(PiD, margin.table(PiD,2)) > > Now I would like to add the Column Name and Row Name "Total" to the last row > and last column (where the marginal totals now hang out). When I use the > command > > colnames(PiD[,4]) <- c("total") I get the error: > > Error in `colnames<-`(`*tmp*`, value = "WTF") : > ?attempt to set colnames on object with less than two dimensions > > > Since I am still new to R, I can't really understand the error... Can > someone tell me what I am doing wrong here? >-- Sarah Goslee http://www.functionaldiversity.org
Hi. Have a look also at ?addmargins. addmargins(table(fS,fF),c(1,2),FUN=list(total.row=sum,total.col=sum)) Andrija On Fri, Apr 15, 2011 at 3:29 PM, Dmitry Berman <ravenblur@gmail.com> wrote:> Listers, > > I have created a cross-tab matrix using the following code: > > S <- > > c(1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,1,2,3,1,1,2,3,1,2,2) > F <- > > c(1,2,3,1,2,3,1,1,1,1,2,3,1,1,3,2,2,2,1,2,3,1,1,1,1,2,3,3,1,3,1,3,1,1,2,3,1,1,3,2,3,2,1,1,1,2,3,1,1,2) > table(S,F) > fF <-factor(F, labels = c("Dem","Ind","Rep")) > fS <-factor(S, labels = c("Dem","Ind","Rep")) > PiD <- table(fS,fF) > PiD <- cbind(PiD, margin.table(PiD,1)) > PiD <- rbind(PiD, margin.table(PiD,2)) > > Now I would like to add the Column Name and Row Name "Total" to the last > row > and last column (where the marginal totals now hang out). When I use the > command > > colnames(PiD[,4]) <- c("total") I get the error: > > Error in `colnames<-`(`*tmp*`, value = "WTF") : > attempt to set colnames on object with less than two dimensions > > > Since I am still new to R, I can't really understand the error... Can > someone tell me what I am doing wrong here? > > Thanks > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]