R filtering a dataframe for a proportion of columns meeting criteria -
i'm sure answer question out there already, can't find it, since i'm beginner @ r , don't know search terms use.
i want retrieve rows in data frame given proportion of columns meet criteria. example, 2/3 columns >1.3.
here have far:
a<-c(1.1,1.2,1.3,1.4,1.5) b<-c(1.3,1.4,1.5,1.6,1.7) c<-c(1.5,1.6,1.7,1.8,1.9) data<-data.frame(a,b,c) data` b c 1 1.1 1.3 1.5 2 1.2 1.4 1.6 3 1.3 1.5 1.7 4 1.4 1.6 1.8 5 1.5 1.7 1.9 c<-function(x) (length(x[(x>1.4)]))>=(2/3*ncol(data)) d<-apply(data,1,c) result<-data[d,] result b c 3 1.3 1.5 1.7 4 1.4 1.6 1.8 5 1.5 1.7 1.9
this works, feel there must simpler way, or function written differently? i'm still trying undestand whole function-thing.
of course, in reality dataframe have alot of columns.
/grateful beginner
maybe (should more efficient rowsums
vectorized , saves need in using apply
loop)
data[rowsums(data > 1.4) >= 2/3*ncol(data),] ## b c ## 3 1.3 1.5 1.7 ## 4 1.4 1.6 1.8 ## 5 1.5 1.7 1.9
or if prefer function, try
myfunc <- function(x) x[rowsums(x > 1.4) >= 2/3*ncol(x), ] myfunc(data) ## b c ## 3 1.3 1.5 1.7 ## 4 1.4 1.6 1.8 ## 5 1.5 1.7 1.9
Comments
Post a Comment