[R] manipulating R contingency tables

arun smartpink111 at yahoo.com
Sat Apr 6 21:13:49 CEST 2013


Hi,
You could also do:
tbl1[,-match("B2",colnames(tbl1))]
#      gts
#labels A1  G3
 #    1 21 120
  #   2 23   0
#or
tbl1[,!grepl("B2",colnames(tbl1))]
#      gts
#labels A1  G3
 #    1 21 120
  #   2 23   0

If you need to remove columns that contains 0 along with removing a specific column.
 tbl1[,!grepl("B2",colnames(tbl1)) & colSums(tbl1==0)==0]
# 1  2 
#21 23 
 tbl1[,colSums(tbl1==0)==0]
#      gts
#labels A1  B2
 #    1 21 127
  #   2 23 112
A.K.





----- Original Message -----
From: arun <smartpink111 at yahoo.com>
To: Abhishek Pratap <abhishek.vit at gmail.com>
Cc: R help <r-help at r-project.org>
Sent: Saturday, April 6, 2013 11:45 AM
Subject: Re: [R] manipulating R contingency tables

Hi,
Try this:
tbl1<- structure(c(21L, 23L, 127L, 112L, 120L, 0L), .Dim = 2:3, .Dimnames = structure(list(
    labels = c(1, 2), gts  = c("A1", "B2", "G3")), .Names = c("labels", 
"gts")), class = "table")
dat1<-as.data.frame(tbl1,stringsAsFactors=FALSE)
dat2<-dat1[dat1$gts!="B2" & dat1$Freq!=0,]


library(reshape2)
 dcast(dat2,labels~gts,value.var="Freq")
#  labels A1  G3
#1      1 21 120
#2      2 23  NA
A.K.




----- Original Message -----
From: Abhishek Pratap <abhishek.vit at gmail.com>
To: "r-help at r-project.org" <r-help at r-project.org>
Cc: 
Sent: Saturday, April 6, 2013 2:55 AM
Subject: [R] manipulating R contingency tables

Hi Guys

I am back with another thing that's puzzling me.

I am creating contingency tables but then I want to filter out certain
columns and also find if any entry in the table is 0.

Example:
        gts
labels  A1  B2  G3
     1    21 127 120
     2    23 112  0

Here I want to remove B2 column from this table and also if any entry is 0
in this case G3 second row.

Missing out on how to do this in an efficient manner as I have to do this
millions of times for my data.

Thanks!
-Abhi

    [[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.



More information about the R-help mailing list