How to get rid of the ending of multiple terms within a single column

I have one column "x" that has a bunch of terms. The terms are all different (EX. 123_pro, 125_pro, 948_pro), however, they all have the same "_pro" suffix. How can I get rid of the "_pro" suffix from all the terms that exist within this single column so that it just become 123, 125, 948, etc. I've included a dput.

> dput(table)
structure(list(X = c("128_pro", "123_pro", "234_pro", "938_pro", 
"122_pro", "232_pro", "111_pro", "092_pro", "099_pro")), class = c("spec_tbl_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -9L), spec = structure(list(
    cols = list(X = structure(list(), class = c("collector_character", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), skip = 1), class = "col_spec"))
suppressPackageStartupMessages({
  library(stringr)
})
my_df <- structure(list(X = c(
  "128_pro", "123_pro", "234_pro", "938_pro",
  "122_pro", "232_pro", "111_pro", "092_pro", "099_pro"
)), class = c(
  "spec_tbl_df",
  "tbl_df", "tbl", "data.frame"
), row.names = c(NA, -9L), spec = structure(list(
  cols = list(X = structure(list(), class = c(
    "collector_character",
    "collector"
  ))), default = structure(list(), class = c(
    "collector_guess",
    "collector"
  )), skip = 1
), class = "col_spec")
)

pattern <- "_pro"
str_remove_all(my_df$X,pattern)
#> [1] "128" "123" "234" "938" "122" "232" "111" "092" "099"

Created on 2020-11-09 by the reprex package (v0.3.0.9001)

If you would like to understand working with data (the tidy way, as this forum promotes)
You could study the book https://r4ds.had.co.nz/
Particularly chapter 5

suppressPackageStartupMessages({
  library(tidyverse)
})
my_df <- tibble::tribble(
  ~X,
  "128_pro",
  "123_pro",
  "234_pro",
  "938_pro",
  "122_pro",
  "232_pro",
  "111_pro",
  "092_pro",
  "099_pro"
)
pattern <- "_pro"
(my_df2 <- mutate(my_df,
                 x_new =str_remove_all(X,pattern)))
... -> new_name

What did you do precisely?.
Share the code

Most R functions do not perform in-place modifications, they return a new object instead, so, if you want to overwrite the original dataframe to include the changes, you have to do it explicitly.

pattern <- "_pro"
my_df$X <- str_remove_all(my_df$X,pattern)
2 Likes

This was what I was missing! Thank you very much!

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.

Hello! This shows the new names without "_pro" in the console (as shown in your comment at the bottom), but when I look back at the actual dataframe, it looks like the terms still have the "_pro" after them and were not changed

pattern <- "_pro"
str_remove_all(my_df$X,pattern)

I've done this, and inserted a new name (df_new), yet my_df still has the _pro endings, despite the console showing the new, correct terms without the _pro in them.

Hello. I've tried doing this: Unfortunately, it only seems to show up in the console, but not in the actual dataframe, which still has the _pro ending. I've your suggestion as well, but that did not seem to work either. Thank you for your help!

pattern <- "_pro"
str_remove_all(my_df$X,pattern)