Home > OS >  Multiply every new rows created by `separate_rows`
Multiply every new rows created by `separate_rows`

Time:03-10

I am using the separate_rows function from tidyr.

Essentially, I would like to change the value of the data that is copied -- in the example below, it would read: "everytime a new row is created, multiply z by 0.5"

I already added an index in the default df. so it could be "everytime the index N is the same as [-1], multiply z by 0.5"

df <- tibble(
  x = 1:4,
  y = c("a", "b,c,d", "e,f"),
  z = 1:4
)

# A tibble: 3 x 3
      x y         z
  <int> <chr> <int>
1     1 a         1
2     2 b,c,d     2
3     3 e,f       3

what we get:

> separate_rows(df, y)
# A tibble: 6 x 3
      x y         z
  <int> <chr> <int>
1     1 a         1
2     2 b         2
3     2 c         2
4     2 d         2
5     3 e         3
6     3 f         3

what I would need (the z values that have a new row multipled by 0.5:

# A tibble: 6 x 3
      x y         z
  <int> <chr> <int>
1     1 a         1
2     2 b         1
3     2 c         1
4     2 d         1
5     3 e         1.5
6     3 f         1.5

CodePudding user response:

You can group by z and multiply if n > 1.

df %>% 
  separate_rows(y) %>% 
  group_by(z) %>% 
  mutate(z = ifelse(n() > 1, z*0.5, z))

      x y         z
  <int> <chr> <dbl>
1     1 a       1  
2     2 b       1  
3     2 c       1  
4     2 d       1  
5     3 e       1.5
6     3 f       1.5

CodePudding user response:

An option is also to multiply 'z' by 0.5, get the pmax with 1 and then use separate_rows

library(dplyr)
library(tidyr)
df %>%
   mutate(z = pmax(1, z * 0.5)) %>% 
   separate_rows(y)

-output

# A tibble: 6 × 3
      x y         z
  <int> <chr> <dbl>
1     1 a       1  
2     2 b       1  
3     2 c       1  
4     2 d       1  
5     3 e       1.5
6     3 f       1.5
  • Related