0
$\begingroup$

I have a data frame of raw read counts, genes in rows and samples in columns

> dput(head(oc))
structure(list(sample1 = c(13L, 0L, 1L, 42L, 15L, 1137L), sample2 = c(21L, 
2L, 20L, 51L, 5L, 203L), sample3 = c(25L, 2L, 6L, 71L, 36L, 1346L
), sample4 = c(59L, 3L, 8L, 58L, 42L, 3428L), sample5 = c(19L, 
4L, 8L, 52L, 1L, 201L)), row.names = c("WASH7P", "MIR6859-1", 
"CICP27", "WASH9P", "MTND1P23", "MTND2P28"), class = "data.frame")
> 

> head(oc)
          sample1 sample2 sample3 sample4 sample5
WASH7P         13      21      25      59      19
MIR6859-1       0       2       2       3       4
CICP27          1      20       6       8       8
WASH9P         42      51      71      58      52
MTND1P23       15       5      36      42       1
MTND2P28     1137     203    1346    3428     201
> 

I just want to keep genes with at least 10 reads in each sample, I mean even if a gene has less than 10 reads in one sample and the rest of four samples have 10 reads, this gene should be removed. For instance here the second, third and fifth rows should be removed

I tied this but all I obtained zero

> df=t(oc)
> cutoff <- 10
> 
> # encode the data based on the cutoff
> df[df >= cutoff] <- 1
> df[df < cutoff] <- 0
> View(df)
> rowSums(df)
sample1 sample2 sample3 sample4 sample5 
      0       0       0       0       0 
> 

> apply(df >= 5, 1, sum)
sample1 sample2 sample3 sample4 sample5 
      0       0       0       0       0 
> apply(df >= 1, 1, sum)
sample1 sample2 sample3 sample4 sample5 
      0       0       0       0       0 
> 

Any suggestion please?

$\endgroup$
0

1 Answer 1

1
$\begingroup$

apply(my_df >= 10, 1, sum) would give you that how many of the samples satisfy your criteria. Basically you apply() the function sum() to the rows (denoted as 1). The sum() does the trick that @Devon Ryan suggested above.

$\endgroup$
3
  • $\begingroup$ I get all zero this is weird > apply(df >= 5, 1, sum) sample1 sample2 sample3 sample4 sample5 0 0 0 0 0 > apply(df >= 1, 1, sum) sample1 sample2 sample3 sample4 sample5 0 0 0 0 0 > $\endgroup$
    – Mahta Mira
    Commented Aug 5, 2021 at 17:22
  • $\begingroup$ Check your df, looks like you have manipulated that data frame. $\endgroup$
    – haci
    Commented Aug 5, 2021 at 17:26
  • 1
    $\begingroup$ No just df=t(oc) I used in the code $\endgroup$
    – Mahta Mira
    Commented Aug 5, 2021 at 17:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.