Adding a string to every other row of a column

Hi,
I wonder how I can add a string such as _2 to every other row for the word column. This means the second repetition of any word is coded by _2

           stim    word cog_id cog  percent  val      label id position
1 abdul_mohd_3-  su3uud  cog_3 cog      01  774.9970     3  1      med
2 abdul_mohd_3-  su3uud  cog_3 cog      01  696.1061     3  2      med
3  abdul_mohd_f  faatiH  cog_f cog      01  720.3641     f  3      ini
4  abdul_mohd_f  faatiH  cog_f cog      01  704.4937     f  4      ini
5  abdul_mohd_T t-aamir  cog_T cog      01 1004.3191     T  5      ini
6  abdul_mohd_T t-aamir  cog_T cog      01  714.7134     T  6      ini
7  abdul_mohd_D d-aabil  cog_D cog      01  864.7862     D  7      ini
8  abdul_mohd_D d-aabil  cog_D cog      01  700.6315     D  8      ini

The output I am looking for is similar to this.

           stim    word cog_id cog  percent  val      label id position
1 abdul_mohd_3-  su3uud   cog_3 cog      01  774.9970     3  1      med
2 abdul_mohd_3-  su3uud_2 cog_3 cog      01  696.1061     3  2      med
3  abdul_mohd_f  faatiH   cog_f cog      01  720.3641     f  3      ini
4  abdul_mohd_f  faatiH_2 cog_f cog      01  704.4937     f  4      ini
5  abdul_mohd_T t-aamir   cog_T cog      01 1004.3191     T  5      ini
6  abdul_mohd_T t-aamir_2 cog_T cog      01  714.7134     T  6      ini
7  abdul_mohd_D d-aabil   cog_D cog      01  864.7862     D  7      ini
8  abdul_mohd_D d-aabil_2 cog_D cog      01  700.6315     D  8      ini

This is the data:

data <- structure(list(stim = c("abdul_mohd_3-", "abdul_mohd_3-", "abdul_mohd_f", 
                            "abdul_mohd_f", "abdul_mohd_T", "abdul_mohd_T", "abdul_mohd_D", 
                            "abdul_mohd_D"), word = c("su3uud", "su3uud", "faatiH", "faatiH", 
                                                      "t-aamir", "t-aamir", "d-aabil", "d-aabil"), cog_id = c("cog_3", 
                                                                                                              "cog_3", "cog_f", "cog_f", "cog_T", "cog_T", "cog_D", "cog_D"
                                                      ), cog = c("cog", "cog", "cog", "cog", "cog", "cog", "cog", "cog"
                                                      ), percent = c("01", "01", "01", "01", "01", "01", "01", "01"
                                                      ), val = c(774.997032242575, 696.106140426446, 720.364098722029, 
                                                                 704.493709576387, 1004.31906059123, 714.713358502878, 864.786221065522, 
                                                                 700.631478305007), label = c("3", "3", "f", "f", "T", "T", "D", 
                                                                                              "D"), id = 1:8, position = c("med", "med", "ini", "ini", "ini", 
                                                                                                                           "ini", "ini", "ini")), row.names = c(NA, 8L), class = "data.frame")

Thank you for your time and help in advance!

paste0() will recycle a vector to match the length of the longer input.

library(tidyverse)
#> Warning: package 'tibble' was built under R version 4.1.2
data <- structure(list(stim = c("abdul_mohd_3-", "abdul_mohd_3-", "abdul_mohd_f", 
                                "abdul_mohd_f", "abdul_mohd_T", "abdul_mohd_T", "abdul_mohd_D", 
                                "abdul_mohd_D"), word = c("su3uud", "su3uud", "faatiH", "faatiH", 
                                                          "t-aamir", "t-aamir", "d-aabil", "d-aabil"), cog_id = c("cog_3", 
                                                                                                                  "cog_3", "cog_f", "cog_f", "cog_T", "cog_T", "cog_D", "cog_D"
                                                          ), cog = c("cog", "cog", "cog", "cog", "cog", "cog", "cog", "cog"
                                                          ), percent = c("01", "01", "01", "01", "01", "01", "01", "01"
                                                          ), val = c(774.997032242575, 696.106140426446, 720.364098722029, 
                                                                     704.493709576387, 1004.31906059123, 714.713358502878, 864.786221065522, 
                                                                     700.631478305007), label = c("3", "3", "f", "f", "T", "T", "D", 
                                                                                                  "D"), id = 1:8, position = c("med", "med", "ini", "ini", "ini", 
                                                                                                                               "ini", "ini", "ini")), row.names = c(NA, 8L), class = "data.frame")
data <- data |> mutate(word=paste0(word,c("","_2")))
data
#>            stim      word cog_id cog percent       val label id position
#> 1 abdul_mohd_3-    su3uud  cog_3 cog      01  774.9970     3  1      med
#> 2 abdul_mohd_3-  su3uud_2  cog_3 cog      01  696.1061     3  2      med
#> 3  abdul_mohd_f    faatiH  cog_f cog      01  720.3641     f  3      ini
#> 4  abdul_mohd_f  faatiH_2  cog_f cog      01  704.4937     f  4      ini
#> 5  abdul_mohd_T   t-aamir  cog_T cog      01 1004.3191     T  5      ini
#> 6  abdul_mohd_T t-aamir_2  cog_T cog      01  714.7134     T  6      ini
#> 7  abdul_mohd_D   d-aabil  cog_D cog      01  864.7862     D  7      ini
#> 8  abdul_mohd_D d-aabil_2  cog_D cog      01  700.6315     D  8      ini

Created on 2022-03-06 by the reprex package (v2.0.1)

1 Like

Amazing!
I don't know how to thank you enough, @FJCC!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.