Home > database >  How to modify a specific range of rows (elements) for a column with dplyr in R language?
How to modify a specific range of rows (elements) for a column with dplyr in R language?

Time:04-28

For example this simple dataframe I want to use dplyr %>% mutate() to edit the y2021 column only the last 5 elements in this row

I want to use ifelse() for the last five elements in this columns instead of doing it with mutate() for all elements in y2021

df01 = data.frame( y2020 = c("Liam", "Olivia", "Emma", "William", "Benjamin", 
                     "Henry", "Isabella", "Evelyn", "Alexander", "Lucas", "Elijah", "Harper"),
            y2021 = c( "William", "Benjamin", "Liam", "Alexander", 'Lucas',
                      'Olivia', 'Henry', 'Emma',  'Harper', "Isabella", "Elijah", 'Evelyn' )
)

CodePudding user response:

library(tidyverse)
df01 %>% 
  mutate(y2021 = if_else(row_number()>(n()-5),"NewValue", y2021))

Output:

       y2020     y2021
1       Liam   William
2     Olivia  Benjamin
3       Emma      Liam
4    William Alexander
5   Benjamin     Lucas
6      Henry    Olivia
7   Isabella     Henry
8     Evelyn  NewValue
9  Alexander  NewValue
10     Lucas  NewValue
11    Elijah  NewValue
12    Harper  NewValue

CodePudding user response:

In base R, we can use replace:

df01$y2021 <- replace(df01$y2021, tail(seq_along(df01$y2021), 5), "NewValue")

We can also use this in dplyr:

library(dplyr)

df01 %>%
  mutate(y2021 = replace(y2021, tail(seq_along(y2021), 5), "NewValue"))

Or we can use an index to replace the last 5 rows:

df01$y2021[length(df01$y2021) - (4:0)] <- "NewValue"

Output

       y2020     y2021
1       Liam   William
2     Olivia  Benjamin
3       Emma      Liam
4    William Alexander
5   Benjamin     Lucas
6      Henry    Olivia
7   Isabella     Henry
8     Evelyn  NewValue
9  Alexander  NewValue
10     Lucas  NewValue
11    Elijah  NewValue
12    Harper  NewValue
  • Related