1

In this dataframe df of 1 column X the value a is occuring multiple times:

dataframe:

df <- structure(list(col = c("<NA>", "<NA>", "a", "<NA>", "<NA>", "<NA>", 
                       "a", "<NA>", "<NA>", "<NA>", "<NA>", "<NA>", "a", "<NA>", "<NA>", 
                       "a", "<NA>", "<NA>", "a", "<NA>", "<NA>", "a", "<NA>")), class = "data.frame", row.names = c(NA, 
                                                                                                                    -23L))

I would like to rename the first a as start and the second a as end and the next a again as start and the next a as end and so on....

desired output:

df1 <- structure(list(col = c("<NA>", "<NA>", "start", "<NA>", "<NA>", 
                       "<NA>", "end", "<NA>", "<NA>", "<NA>", "<NA>", "<NA>", "start", 
                       "<NA>", "<NA>", "end", "<NA>", "<NA>", "start", "<NA>", "<NA>", 
                       "end", "<NA>")), class = "data.frame", row.names = c(NA, -23L
                       ))

Many thanks!!!

NelsonGon
  • 13,015
  • 7
  • 27
  • 57
TarJae
  • 72,363
  • 6
  • 19
  • 66

2 Answers2

4

Take benefit of R's recycling property.

df$col[df$col == 'a'] <- c('start', 'end')
df

#     col
#1   <NA>
#2   <NA>
#3  start
#4   <NA>
#5   <NA>
#6   <NA>
#7    end
#8   <NA>
#9   <NA>
#10  <NA>
#11  <NA>
#12  <NA>
#13 start
#14  <NA>
#15  <NA>
#16   end
#17  <NA>
#18  <NA>
#19 start
#20  <NA>
#21  <NA>
#22   end
#23  <NA>
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
1

additional solution option

df <- structure(list(col = c("<NA>", "<NA>", "a", "<NA>", "<NA>", "<NA>", 
                             "a", "<NA>", "<NA>", "<NA>", "<NA>", "<NA>", "a", "<NA>", "<NA>", 
                             "a", "<NA>", "<NA>", "a", "<NA>", "<NA>", "a", "<NA>")), class = "data.frame", row.names = c(NA, 
                                                                                                                    -23L))

library(dplyr)
library(data.table)

df %>% 
  mutate(col = ifelse(col == "a" & data.table::rowid(col == "a") %% 2 == 0, "end", col))
#>     col
#> 1  <NA>
#> 2  <NA>
#> 3     a
#> 4  <NA>
#> 5  <NA>
#> 6  <NA>
#> 7   end
#> 8  <NA>
#> 9  <NA>
#> 10 <NA>
#> 11 <NA>
#> 12 <NA>
#> 13    a
#> 14 <NA>
#> 15 <NA>
#> 16  end
#> 17 <NA>
#> 18 <NA>
#> 19    a
#> 20 <NA>
#> 21 <NA>
#> 22  end
#> 23 <NA>

setDT(df)[col == "a", col := ifelse(rowid(col) %% 2 == 0, "end", col)][]
#>      col
#>  1: <NA>
#>  2: <NA>
#>  3:    a
#>  4: <NA>
#>  5: <NA>
#>  6: <NA>
#>  7:  end
#>  8: <NA>
#>  9: <NA>
#> 10: <NA>
#> 11: <NA>
#> 12: <NA>
#> 13:    a
#> 14: <NA>
#> 15: <NA>
#> 16:  end
#> 17: <NA>
#> 18: <NA>
#> 19:    a
#> 20: <NA>
#> 21: <NA>
#> 22:  end
#> 23: <NA>
#>      col

Created on 2021-04-19 by the reprex package (v2.0.0)

Yuriy Saraykin
  • 8,390
  • 1
  • 7
  • 14