heisenbug29
heisenbug29

Reputation: 165

Replace multiple specific rows in a column using dplyr groupwise in r

Following is my InputDataSet dataset -

Group   Time    Speed
1       5       25 # Ignore first 3 elements
1       10      23 # Ignore first 3 elements
1       15      21 # Ignore first 3 elements
1       20      33 # Speed - 5 
1       25      40 # Speed - 5
1       30      42 # Speed - 5
1       35      52 # Speed - 5
1       40      48 # Speed - 5
1       45      52 # Speed - 5
2       5       48 # Ignore first 3 elements
2       10      42 # Ignore first 3 elements
2       15      39 # Ignore first 3 elements
2       20      36 # Speed - 5
2       25      38 # Speed - 5
2       30      46 # Speed - 5
2       35      53 # Speed - 5
3       5       45 # Speed - 5
3       10      58 # Speed - 5

In every group after the 3rd element in column speed, I would like to subtract 5 from its speed value and replace in that column. How can I do this using dplyr?

This is what I have tried and always gives errors and cannot find a way to use replace() function here.

InputDataSet <- InputDataSet %>% group_by(Group) %>% mutate(
  Speed[-c(1:3)] = Speed[-c(1:3)] - 5)

The above approach works with vectors, but when I try to do this using dplyr in a column it gives me -

Error: unexpected '=' in:
" 
  Speed[-c(1:3)] ="

Upvotes: 0

Views: 241

Answers (2)

akrun
akrun

Reputation: 886938

We can use case_when

library(dplyr)
library(data.table)
df %>%
    mutate(Speed = rowid(Group) < 4 ~ Speed, TRUE ~ Speed - 5))

Upvotes: 0

Chris Ruehlemann
Chris Ruehlemann

Reputation: 21400

Here's a dplyr approach via row_numbers:

library(dplyr)
df %>%
  group_by(Group) %>%
  mutate(
    r_num = row_number(),
    Speed = ifelse(r_num %in% 1:3, Speed, Speed -5))
# A tibble: 20 x 3
# Groups:   Group [2]
   Group Speed r_num
   <dbl> <dbl> <int>
 1     1  9.44     1
 2     1  9.77     2
 3     1 11.6      3
 4     1  5.07     4
 5     1  5.13     5
 6     1  6.72     6
 7     1  5.46     7
 8     1  3.73     8
 9     1  4.31     9
10     1  4.55    10
11     2 11.2      1
12     2 10.4      2
13     2 10.4      3
14     2  5.11     4
15     2  4.44     5
16     2  6.79     6
17     2  5.50     7
18     2  3.03     8
19     2  5.70     9
20     2  4.53    10

Data:

set.seed(123)
df <- data.frame(
  Group = c(rep(1,10), rep(2,10)),
  Speed = rnorm(20, 10)
)

Upvotes: 2

Related Questions