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]]
Hello,
Try the following. The first function removes a column(s) from the
table, and the secondd all rows and columns with zero elements in them.
fun1 <- function(x, col) x[, -which(colnames(x) %in% col)]
fun2 <- function(x){
idx <- which(x == 0, arr.ind = TRUE)
x[-idx[, 1], -idx[, 2]]
}
Hope this helps,
Rui Barradas
Em 06-04-2013 07:55, Abhishek Pratap escreveu:> 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.
>
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.