-2

I have data frame like this

Subject      stim       GZMB   TNF  IFNg      
HC           no stim    1        1     0
EC           1100       0        1     0
CH           no stim    0        0     1

I would like to subset the data such that I get the subject and stim values for which GZMB is 1 and all the others (TNF, IFNg) are zero.

Mako212
  • 6,787
  • 1
  • 18
  • 37
Taliman
  • 57
  • 7
  • 4
    Please [edit your question as shown here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – NelsonGon Jun 04 '19 at 15:58

1 Answers1

1

With dplyr, you could use:

library(dplyr)
df %>% 
        mutate(Sum=colSums(.[,-c(1,2)])) %>% 
         filter(Sum==1  & GZMB==1)
  Subject stim GZMB TNF IFNg Sum
1    1100    0    1   0   NA   1

Data:

df<-structure(list(Subject = c("no", "1100", "no"), stim = c("stim", 
"0", "stim"), GZMB = c(1L, 1L, 0L), TNF = c(1L, 0L, 0L), IFNg = c(0L, 
NA, 1L)), class = "data.frame", row.names = c("HC", "EC", "CH"
))
NelsonGon
  • 13,015
  • 7
  • 27
  • 57
  • Thanks.Can you explain (.[,-c(1,2)]) part of the code? – Taliman Jun 04 '19 at 21:19
  • This takes column sums except for the first and second columns. `.`. That is similar to `mydata[,-c(1,2)]` ie select minus the first and second columns. – NelsonGon Jun 05 '19 at 05:08