Adding unique numerics to character strings

Viewed 42

I wish to append a numeric onto a character vector, so that each row receives a unique numeric different to the previous, though +1 relative to the previous. Such that n+1.

I have tried:

test.all$ly_name <- sub("^", 1:nrow(test.all$ly_name), test.all$ly_name )

What it looks like:

1Prairie_Potholes          
1Prairie_Potholes          
1Prairie_Potholes 
.
.
.

expected:

Prairie_Potholes1          
Prairie_Potholes2          
Prairie_Potholes3 
.
.
.

However, this only assign the first numeric, 1.

reproducible code:

c("Prairie_Potholes", "Prairie_Potholes", "Prairie_Potholes", 
"Prairie_Potholes", "Prairie_Potholes", "Prairie_Potholes", "Prairie_Potholes", 
"Prairie_Potholes", "Prairie_Potholes", "Boreal_Hardwood_Transition", 
"Boreal_Hardwood_Transition", "Boreal_Hardwood_Transition", "Boreal_Hardwood_Transition", 
"Boreal_Hardwood_Transition", "Boreal_Hardwood_Transition")
2 Answers

Unique within repeats

make.unique(c("Prairie_Potholes", "Prairie_Potholes", "Prairie_Potholes", 
              "Prairie_Potholes", "Prairie_Potholes", "Prairie_Potholes", "Prairie_Potholes", 
              "Prairie_Potholes", "Prairie_Potholes", "Boreal_Hardwood_Transition", 
              "Boreal_Hardwood_Transition", "Boreal_Hardwood_Transition", "Boreal_Hardwood_Transition", 
              "Boreal_Hardwood_Transition", "Boreal_Hardwood_Transition"), sep = "")
 [1] "Prairie_Potholes"            "Prairie_Potholes1"           "Prairie_Potholes2"           "Prairie_Potholes3"           "Prairie_Potholes4"          
 [6] "Prairie_Potholes5"           "Prairie_Potholes6"           "Prairie_Potholes7"           "Prairie_Potholes8"           "Boreal_Hardwood_Transition" 
[11] "Boreal_Hardwood_Transition1" "Boreal_Hardwood_Transition2" "Boreal_Hardwood_Transition3" "Boreal_Hardwood_Transition4" "Boreal_Hardwood_Transition5"

add a suffix to every row

x <- c("Prairie_Potholes", "Prairie_Potholes", "Prairie_Potholes", 
  "Prairie_Potholes", "Prairie_Potholes", "Prairie_Potholes", "Prairie_Potholes", 
  "Prairie_Potholes", "Prairie_Potholes", "Boreal_Hardwood_Transition", 
  "Boreal_Hardwood_Transition", "Boreal_Hardwood_Transition", "Boreal_Hardwood_Transition", 
  "Boreal_Hardwood_Transition", "Boreal_Hardwood_Transition")

x <- paste0(x, 1:length(x))
x
 [1] "Prairie_Potholes1"            "Prairie_Potholes2"            "Prairie_Potholes3"            "Prairie_Potholes4"            "Prairie_Potholes5"           
 [6] "Prairie_Potholes6"            "Prairie_Potholes7"            "Prairie_Potholes8"            "Prairie_Potholes9"            "Boreal_Hardwood_Transition10"
[11] "Boreal_Hardwood_Transition11" "Boreal_Hardwood_Transition12" "Boreal_Hardwood_Transition13" "Boreal_Hardwood_Transition14" "Boreal_Hardwood_Transition15"

You should use paste0 and seq_along. The seq_along function generates a vector of integers the same length as the input vector. The paste0 function pastes the strings together elementwise with no space between.

ly_name <- c("Prairie_Potholes", "Prairie_Potholes", "Prairie_Potholes", 
             "Prairie_Potholes", "Prairie_Potholes", "Prairie_Potholes", "Prairie_Potholes", 
             "Prairie_Potholes", "Prairie_Potholes", "Boreal_Hardwood_Transition", 
             "Boreal_Hardwood_Transition", "Boreal_Hardwood_Transition", "Boreal_Hardwood_Transition", 
             "Boreal_Hardwood_Transition", "Boreal_Hardwood_Transition")

paste0(ly_name, seq_along(ly_name))

 [1] "Prairie_Potholes1"            "Prairie_Potholes2"            "Prairie_Potholes3"           
 [4] "Prairie_Potholes4"            "Prairie_Potholes5"            "Prairie_Potholes6"           
 [7] "Prairie_Potholes7"            "Prairie_Potholes8"            "Prairie_Potholes9"           
[10] "Boreal_Hardwood_Transition10" "Boreal_Hardwood_Transition11" "Boreal_Hardwood_Transition12"
[13] "Boreal_Hardwood_Transition13" "Boreal_Hardwood_Transition14" "Boreal_Hardwood_Transition15"

If you happen to want the numbers to restart at 1 every time there is a new ly_name, you can use group_by from dplyr.

library(dplyr)
df <- data.frame(ly_name)
df %>%
  group_by(ly_name) %>%
  mutate(ly_name = paste0(ly_name, seq_along(ly_name)))

# A tibble: 15 x 1
# Groups:   ly_name [15]
   ly_name                    
   <chr>                      
 1 Prairie_Potholes1          
 2 Prairie_Potholes2          
 3 Prairie_Potholes3          
 4 Prairie_Potholes4          
 5 Prairie_Potholes5          
 6 Prairie_Potholes6          
 7 Prairie_Potholes7          
 8 Prairie_Potholes8          
 9 Prairie_Potholes9          
10 Boreal_Hardwood_Transition1
11 Boreal_Hardwood_Transition2
12 Boreal_Hardwood_Transition3
13 Boreal_Hardwood_Transition4
14 Boreal_Hardwood_Transition5
15 Boreal_Hardwood_Transition6
Related