vectorization - Applying vectorized subsetting across multiple columns in R -


i try find straight-forward way vectorize/generalize subsetting of data.frame. let's assume have data.frame:

df <- data.frame(a = 1:5, b = 10 * 1:5, c = 100 * 1:5) 

every column has own condition , goal subset df rows remain condition met @ least 1 column. want find vectorized subset mechanism generalizes

df <- subset(df, df[,1]<2 | df[,2]< 30 | df[,3]<100) 

so formulate this

crit <- c(2,30,100) df <- subset(df, df$header < crit[1:3]) 

and down road want to.

df <- subset(df, df$header < crit[1:n]) 

i know multi-step loop workaround, there must way. grateful help.

given:

x <- c(1:5) y <- c(10,20,30,40,50) z <- c(100,200,300,400,500)  # df base function mydf <- data.frame(a = x, b = y, c = z)  crit <- c(2,30,100) 

then let see values in column less crit value:

> sweep(mydf, 2, crit, "<")              b     c [1,]  true  true false [2,] false  true false [3,] false false false [4,] false false false [5,] false false false 

and give rows meet of criteria:

> subset(mydf, rowsums(sweep(mydf, 2, crit, "<")) > 0)     b   c 1 1 10 100 2 2 20 200 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

android - Keyboard hides my half of edit-text and button below it even in scroll view -