Extract "n" rows with least variation across samples

extract imdb
extract movie
extract sentence
extract antonym
extract 2009 cast
extract noun
extract movie download
extract trailer

An example dataframe:

a = c(1,2,3,4,5,6)
b = c(1,1.1,1.2,1.3,1.4,1.5)
c = c(2,4,6,7,9,20)
d = c(1,1,1,1.2,1,1.3)
e = c(6,9,100,20,52,61)
gene = c("gene1","gene2","gene3","gene4","gene5","gene6")
df = data.frame(gene,a,b,c,d,e)

Hi, From the dataframe, I would like to extract "n" number of rows/genes (say, 2) which are most stable across the dataset i.e, with minimal variation in intensities across the samples. How can I do this? Thank you very much


1) Base R This uses onlyu base R:

n <- 3
df[head(order(apply(df[-1], 1, sd)), n), ]

giving:

   gene a   b c   d  e
1 gene1 1 1.0 2 1.0  6
2 gene2 2 1.1 4 1.0  9
4 gene4 4 1.3 7 1.2 20

2) dplyr/apply or using dplyr we have the following.

library(dplyr)

n <- 3
df %>%
   arrange(select(., -gene) %>% apply(1, sd)) %>%
   top_n(-n)

3) dplyr/rowwise or another dplyr solution, this one using rowwise. Note that dot within do refers to the current row as a list and dot within {...} refers to the input of the expression it is in. Omit the select(-sd) line if you want to leave the sd column in.

library(dplyr)

n <- 3
df %>% 
   rowwise %>% 
   do(as.data.frame(.) %>% { mutate(., sd = select(., -gene) %>% sd(.))}) %>%
   ungroup %>%
   arrange(sd) %>%
   select(-sd) %>%
   top_n(-n)

4) dplyr/purrr This is the same as (2) except it uses pmap_dbl from purrr instead of apply.

library(dplyr)
library(purr)

n <- 3
df %>%
   arrange(select(., -gene) %>% pmap_dbl(~ sd(c(...)))) %>%
   top_n(-n)

Extract (film), a preparation containing the active ingredient of a substance in concentrated form. Directed by Mike Judge. With Jason Bateman, Kristen Wiig, Ben Affleck, Mila Kunis. Joel, the owner of an extract plant, tries to contend with myriad personal and professional problems, such as his potentially unfaithful wife and employees who want to take advantage of him.


Here is an option using rowSds from matrixStats

library(matrixStats)
n <- 3
df[order(rowSds(as.matrix(df[-1])))[seq_len(n)],]
#   gene a   b c   d  e
#1 gene1 1 1.0 2 1.0  6
#2 gene2 2 1.1 4 1.0  9
#4 gene4 4 1.3 7 1.2 20

Or using tidyverse

library(tidyverse)
df %>%
   arrange(pmap_dbl(.[-1], ~ sd(c(...)))) %>% 
   slice(seq_len(n))
#   gene a   b c   d  e
#1 gene1 1 1.0 2 1.0  6
#2 gene2 2 1.1 4 1.0  9
#3 gene4 4 1.3 7 1.2 20

Or with rowSds

df %>%
     arrange(rowSds(as.matrix(.[-1]))) %>%
     slice(seq_len(n))
#   gene a   b c   d  e
#1 gene1 1 1.0 2 1.0  6
#2 gene2 2 1.1 4 1.0  9
#3 gene4 4 1.3 7 1.2 20

Or with data.table

library(data.table)
setDT(df)[df[, order(rowSds(as.matrix(.SD)))[seq_len(n)], .SDcols = -1]]
#    gene a   b c   d  e
#1: gene1 1 1.0 2 1.0  6
#2: gene2 2 1.1 4 1.0  9
#3: gene4 4 1.3 7 1.2 20

Extract, The owner of a factory that produces flavor extracts, Joel Reynold (Jason Bateman) seems to have it all, but really doesn't. What's missing is sexual attention from his wife, Suzie (Kristen Wiig). Joel hatches a convoluted plan to get Suzie to Extract definition, to get, pull, or draw out, usually with special effort, skill, or force: to extract a tooth. See more.


Using data.table

library(data.table)

# Helper function
meanad <- function(x) {
  sum(abs(x-mean(x)))/length(x)
}

k <- 3
topgenes <- setDT(df)[, meanad := meanad(unlist(.SD)), by = gene
                      ][order(meanad)
                        ][, as.character(head(gene, k))
                          ]

df[gene %in% topgenes]
    gene a   b c   d  e   meanad
1: gene1 1 1.0 2 1.0  6 1.304444
2: gene2 2 1.1 4 1.0  9 2.159556
3: gene4 4 1.3 7 1.2 20 4.673333

How to Use Extracts for Calorie-Free Flavor in Your Cooking , , an essential flavor ingredient in many baked goods like cookies, brownies, cakes and more. extract - a solution obtained by steeping or soaking a substance (usually in water)


Extract, . decoction, distillation, distillate, abstraction, concentrate, essence, juice, solution, tincture, elixir, quintessence. Extract Critics Consensus. Extract has some very funny moments and several fine performances, but the film feels slighter and more uneven than Mike Judge's previous work.


Extract (2009), noun. something extracted. a passage taken from a book, article, etc.; excerpt; quotation. a solution or preparation containing the active principles of a drug, plant juice, or the like; concentrated solution: vanilla extract. Extract the meat from the shell, and cut it into small pieces. When it is cold break it up, and extract all the flesh from the shell. Lay the lemon-rind in scalding water, to extract the bitterness. Extract all the stones, and spread out the plums on large dishes.


Extract, The idea of “removing” or “pulling away” connects abstract to extract, which stems from Latin through the combination of trahere with the prefix ex-, meaning “out of”​  An extract is a substance made by extracting a part of a raw material, often by using a solvent such as ethanol or water. Extracts may be sold as tinctures, absolutes or in powder form.