0

having a tibbl df:

A B C
1 0 -1
2 1 2
-3 -2 1

I need to filter all columns based on a condition and summarize them to a new variable (I know the legth is different then, but is there an else option for "NA"?

I know it works when I sum them or min/max etc. Summarize all group values and a conditional subset in the same call

df %>% summarise(A_to_B = (A[A < 0]),
B_to_A = (A[A > 0]),
B_to_C = (B[B < 0]),
C_to_B = (B[B > 0]),
C_to_B = (C[C < 0]),
B_to_C = (C[C > 0]))

Hope it is clear what I mean, I want to sort a variable to a new one, if it doesn't fit to the condition it should be NA to have the same length for a resulting tibble/df.

Edit: Expected output:

A_to_B B_to_A B_to_C  ....
NA      1      NA
NA      2      NA
-3      NA     -2

It is litterally a split of each variable (column) based on a condition. Thanks!

Herr Student
  • 853
  • 14
  • 26
  • @zx8754 added the output for the first three conditions, it's more or less a split of the variables based on condition. If necessary, could also include the zero in one of the conditions. – Herr Student Mar 02 '23 at 16:35

1 Answers1

2

Maybe this helps using replace

library(dplyr) # >= 1.1.0, use summarize below

df %>% 
  reframe(across(A:C, ~ replace(.x, !(.x < 0), NA), .names="{.col}_lt_0"), 
          across(A:C, ~ replace(.x, !(.x > 0), NA), .names="{.col}_gt_0")) 
  A_lt_0 B_lt_0 C_lt_0 A_gt_0 B_gt_0 C_gt_0
1     NA     NA     -1      1     NA     NA
2     NA     NA     NA      2      1      2
3     -3     -2     NA     NA     NA      1
Andre Wildberg
  • 12,344
  • 3
  • 12
  • 29