Timothy_Goodman
Timothy_Goodman

Reputation: 421

Realization of a data frame with the smallest values grouped by a column

How can I create a new data frame with the smallest values group by a column.

For example this df:

df <- read.table(header = TRUE, text = 'Gene   Value
A      12
A      10
B      3
B      0
B      6
C      1
D      0
D      4')

Now with:

test <- setDT(df)[, .SD[which.min(Value)], by=Gene]

I get this:

> test
   Gene Value
1:    A    10
2:    B     0
3:    C     1
4:    D     0

But how can I use a second condition for Value > 0 here? I want to have this output:

> test
   Gene Value
1:    A    10
2:    B     3
3:    C     1
4:    D     4

Upvotes: 1

Views: 54

Answers (3)

akrun
akrun

Reputation: 887951

Using aggregate from base R

aggregate(Value ~ Gene, subset(df, Value > 0), min)
#    Gene Value
#1    A    10
#2    B     3
#3    C     1
#4    D     4

Upvotes: 0

DeduciveR
DeduciveR

Reputation: 1702

Using tidyverse you can group, filter and then summarize the min value:

library(tidyverse)

df2 <- df %>% 
  group_by(Gene) %>%
  filter(Value != 0) %>% 
  summarise(Value = min(Value))

# A tibble: 4 x 2
  Gene  Value
  <fct> <dbl>
1 A        10
2 B         3
3 C         1
4 D         4

Upvotes: 2

arg0naut91
arg0naut91

Reputation: 14774

Could do:

setDT(df)[, .(Value = min(Value[Value > 0])), by=Gene]

Output:

   Gene Value
1:    A    10
2:    B     3
3:    C     1
4:    D     4

Upvotes: 4

Related Questions