zoe
zoe

Reputation: 311

apply conditional on two groups of columns within dataframe

I have a df:

a<-c(5,1,5,3,5,3,5,1)
b<-c(1,5,1,5,1,5,3,5)

df<-as.data.frame(rbind(a,b))
names(df)<-c('pre1','post1','pre2','post2','pre3','post3','pre4','post4')

And I have two groups of samples within the columns eg 'pre' and post':

pre<-seq(1,8,by=2)
post<-seq(2,8,by=2)

I would like to apply a conditional that 100% of the pre and 50% of the post pass OR 50% of the pre and 100% of the post

eg

if 100% of 'pre' are 3 or over AND 50% post are 3 or over keep row OR if 50% of 'pre' are 3 or over AND 100% post are 3 or over keep row so in the example df only row 'a' would stay

I have:

test<- ((df[apply(df[pre],1,function(x) sum(x>=3)/length(x)),] & 
         df[apply(df[post],1,function(x) sum(x>3)/length(x))>=0.5,]) | 
        (df[apply(df[pre],1,function(x) sum(x>3)/length(x))>=0.5,] & 
         df[apply(df[post],1,function(x) sum(x>3)/length(x)),]))

But I get a vector of 'TRUEs' which isn't what I want.

Upvotes: 1

Views: 46

Answers (4)

user10191355
user10191355

Reputation:

Here's a base R solution that splits by row name, checks the conditions using sapply, and uses the output as a logical index on df:

df[sapply(split(df, rownames(df)), function(x) {
    (sum(x[pre] > 2)/ncol(x[pre]) >= .5) & (sum(x[post] > 2)/ncol(x[post]) == 1) ||
    (sum(x[pre] > 2)/ncol(x[pre]) == 1) & (sum(x[post] > 2)/ncol(x[post]) >= .5)
}),]

#### OUTPUT ####

  pre1 post1 pre2 post2 pre3 post3 pre4 post4
a    5     1    5     3    5     3    5     1

Upvotes: 0

akrun
akrun

Reputation: 887088

Here is one option with tidyverse

library(tidyverse)
library(rap)
crossing(val = c(0.5, 1), cols = c("pre", "post")) %>%
   rap(x = ~ df %>% 
                 select(matches(cols)) %>%
                 {rowMeans(. >=3) >= val}) %>%
                 group_by(val) %>% 
                 transmute(ind = reduce(x, `&`)) %>% 
                 filter(any(ind)) %>% 
                 pull(ind) %>% 
   filter(df, .)
#  pre1 post1 pre2 post2 pre3 post3 pre4 post4
#1    5     1    5     3    5     3    5     1

Upvotes: 0

Jon Spring
Jon Spring

Reputation: 66445

Here's a much less concise tidyverse solution that could probably be shortened substantially.

library(tidyverse)
pass_val = 3
df %>%
  rownames_to_column() %>%
  gather(col, val, -rowname) %>%
  separate("col", c("type", "num"), sep = -1) %>%
  count(rowname, type, pass = val >= pass_val) %>%
  spread(pass, n, fill = 0) %>%
  transmute(rowname, type, pass_pct = `TRUE`/(`TRUE` + `FALSE`)) %>%
  spread(type, pass_pct) %>%
  filter(post == 1 & pre >= 0.5 | post >= 0.5 & pre == 1)

Upvotes: 2

Ronak Shah
Ronak Shah

Reputation: 388972

We can create a logical vector to compare using rowSums

df[(rowSums(df[pre] >= 3)/length(pre) == 1) & 
    (rowSums(df[post] >= 3)/length(post) >= 0.5) |
    (rowSums(df[post] >= 3)/length(post) == 1) & 
    (rowSums(df[pre] >= 3)/length(pre) >= 0.5), ]

#  pre1 post1 pre2 post2 pre3 post3 pre4 post4
#a    5     1    5     3    5     3    5     1

Using apply we can do

df[apply(df[pre] >= 3, 1, all) & apply(df[post] >= 3, 1, sum)/length(post) >= 0.5 |
   apply(df[post] >= 3, 1, all) & apply(df[pre] >= 3, 1, sum)/length(pre) >= 0.5, ]

Upvotes: 2

Related Questions