Remove Leading & Trailing Zeros in R (5 Examples) | Delete Start & End of String | sub & str_remove

preview_player
Показать описание
R code of this video:

my_values <- c("000123", "055", "01010", "22200") # Create example data
my_values # Print example data

my_values_new1 <- sub("^0+", "", my_values) # Apply sub function
my_values_new1 # Print updated data

library("stringr") # Load stringr package

my_values_new2 <- str_remove(my_values, "^0+") # Apply str_remove function
my_values_new2 # Print updated data

my_values_new3 # Print updated data

my_values_new4 <- sub("0+$", "", my_values) # Apply sub function
my_values_new4 # Print updated data

my_values_new5 <- str_remove(my_values, "0+$") # Apply str_remove function
my_values_new5 # Print updated data

Follow me on Social Media:
Рекомендации по теме
Комментарии
Автор

This saved me working to match census tract data sets that were not matching - thank you so much.

museandmetrics
Автор

Thank you sir for sharing informative videos

eazycooking
Автор

Can you provide an example of doing this with a dataset? I am trying to match PUMA codes and am having some issues

brettneumann