Imagine I have a data table. For instance:
library(data.table)
RRR <-data.table(1:15,runif(15),rgeom(15,0.5),rbinom(15,2,0.5))
V1 V2 V3 V4
1: 1 0.33577273 0 0
2: 2 0.66739739 2 1
3: 3 0.07501655 0 0
4: 4 0.43195663 2 1
5: 5 0.39525841 3 2
6: 6 0.15189738 1 1
7: 7 0.02637279 0 1
8: 8 0.44165623 0 1
9: 9 0.98710570 2 0
10: 10 0.62402805 1 0
11: 11 0.84829465 3 2
12: 12 0.02170976 0 1
13: 13 0.74608925 0 2
14: 14 0.29102296 2 0
15: 15 0.83820646 1 1
How can I get the data.table from it with all the ROWS that contain "0" in any column? (or some value)
If I had to do this with a single column, I could use:
RRR[V4==0,]
V1 V2 V3 V4
1: 1 0.33577273 0 0
2: 3 0.07501655 0 0
3: 9 0.98710570 2 0
4: 10 0.62402805 1 0
5: 14 0.29102296 2 0
But what if I want to do this with all the columns at once, because I have a lot?
It does not do what I need.
RRR[,sapply(RRR,function(xx)(xx==0)), with=TRUE]
V1 V2 V3 V4
[1,] FALSE FALSE TRUE TRUE
[2,] FALSE FALSE FALSE FALSE
[3,] FALSE FALSE TRUE TRUE
[4,] FALSE FALSE FALSE FALSE
[5,] FALSE FALSE FALSE FALSE
[6,] FALSE FALSE FALSE FALSE
[7,] FALSE FALSE TRUE FALSE
[8,] FALSE FALSE TRUE FALSE
[9,] FALSE FALSE FALSE TRUE
[10,] FALSE FALSE FALSE TRUE
[11,] FALSE FALSE FALSE FALSE
[12,] FALSE FALSE TRUE FALSE
[13,] FALSE FALSE TRUE FALSE
[14,] FALSE FALSE FALSE TRUE
[15,] FALSE FALSE FALSE FALSE
Maybe with a for loop and some complicated paste ?. Although, I would prefer to use the simple data.table syntax.
Similarly, how would you get a data table with all the COLUMNS that contain "0" on any row?
I know how to get columns (in general) that fulfill a condition, such as numeric,
RRR[,sapply(RRR,function(xx)is.numeric(xx)),with=FALSE]
, elementwise.
, - , system.time() , , .
set.seed(1)
n <- 1000000
RRR <- data.table(matrix(rgeom(100*n,0.5), ncol=100))
Getting ROWS
> RRR[RRR[,rowSums(RRR==0)>0]]
user system elapsed
2.72 0.55 3.27
> RRR[rowSums(RRR==0)>0]
user system elapsed
2.58 0.70 3.28
> RRR[apply(RRR,MAR=1,function(xx)any(xx==0))]
user system elapsed
10.81 0.19 11.00
> RRR[apply(RRR[,paste0('V',1:ncol(RRR)),with=FALSE],function(xx)any(xx==0),MAR=1)]
user system elapsed
10.49 0.30 10.83
Getting COLUMNS
> RRR[,sapply(RRR,function(xx)any(xx==0)), with=FALSE]
user system elapsed
0.81 0.31 1.12
> `[.listof`(RRR,colSums(RRR==0)>0)
user system elapsed
2.14 0.27 2.41
> RRR[,colSums(RRR==0)>0, with=FALSE]
user system elapsed
2.26 0.48 2.75
> RRR[, .SD, .SDcols=sapply(RRR, function(x) any(x==0))]
user system elapsed
0.78 0.36 1.14
> RRR[, .SD, .SDcols=sapply(RRR, function(x) any(!as.logical(x)))]
user system elapsed
0.41 0.25 0.66
> RRR[Reduce('|',lapply(RRR,function(xx)(xx==0)))]
user system elapsed
3.11 0.33 3.44
> RRR[,apply(RRR[,paste0('V',1:ncol(RRR)),with=FALSE],function(xx)any(xx==0),MAR=2),with=FALSE]
user system elapsed
3.48 0.80 4.28
:
RRR[, i := any(unlist(lapply(.SD, function(x) x==0))), seq_len(nrow(RRR))][i==TRUE][,i:=NULL]
, , "" , , .
.
sapply , .
, .
, (== 0), . , .
.
- sapply (RRR, (xx), (xx == 0))
- a) , , .
- RRR [ "a)" ]
, , .
, RRR[unique(unlist(sapply(RRR,function(xx)which(xx==0))))]
.
RRR[(RRR==0)] <- NA; na.omit(RRR)