0

Hello I have a df such as :

Groups Seq VALUE 
G1 Seq1 0
G1 Seq2 10
G1 Seq1 20
G1 Seq1 2
G1 Seq3 0
G2 Seq1 0
G2 Seq1 0
G2 Seq2 0
G3 Seq1 8
G3 Seq1 7
G4 Seq2 0

and I would like to transform it as :

Seq  G1    G2    G3    G4
Seq1 TRUE  FALSE  TRUE  FALSE
Seq2 TRUE  FALSE FALSE FALSE
Seq3 FALSE FALSE FALSE FALSE

Where groups are colnames and if a any Seq value is > 0 within groups then I put a TRUE, otherwise I put a FALSE

So far I tried this :

as.data.frame(test_df) %>% 
  group_by(Groups,Seq) %>% 
  summarise(
    x=case_when(
      any(Seq > 0) ~ "TRUE",
  ) %>% 
  pivot_wider(names_from = Groups, values_from = x)
chippycentra
  • 3,396
  • 1
  • 6
  • 24

2 Answers2

2

Using pivot_wider directly :

tidyr::pivot_wider(df, names_from = Groups, values_from = Value,
                    values_fn = list(Value = ~any(. > 0)), values_fill = FALSE)

# A tibble: 3 x 5
#  Seq   G1    G2    G3    G4   
#  <chr> <lgl> <lgl> <lgl> <lgl>
#1 Seq1  TRUE  FALSE TRUE  FALSE
#2 Seq2  TRUE  FALSE FALSE FALSE
#3 Seq3  FALSE FALSE FALSE FALSE
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • You do not use the column ```Seq```? In the exemple there is only 3 columns but in the real df there are 20 Columns, and I need to be focuses only on those 3 columns – chippycentra Jul 29 '20 at 12:12
  • You can first `select` the columns which you are interested in or add `id_cols = Seq` in `pivot_wider` in above answer. – Ronak Shah Jul 29 '20 at 12:14
0

Must be VALUE instead of seq in summarise()

Also this could be simplified to summarise(x = any(VALUE > 0)) (not tested)

py_b
  • 189
  • 6