-3

I am doing this in R I have column named "region" which consists of values as below:

region_24,
region_67,
region_30,
region_26,
region_29

I want to remove "region_" from this column.

Can you please help me with the coding using dplyr? using following:

  • filter
  • replace
  • remove
  • delete
  • apply

What are the different various ways of doing this?

MrFlick
  • 195,160
  • 17
  • 277
  • 295
Jatin
  • 25
  • 4

2 Answers2

1
test$region <-  gsub("[region_]", "", test$region)
zx8754
  • 52,746
  • 12
  • 114
  • 209
Jatin
  • 25
  • 4
0

We can use sub :

transform(df, V1 = sub('region_', '', V1))

#  V1
#1 24
#2 67
#3 30
#4 26
#5 29

Or if you want to extract only the numbers from the column, we can use parse_number.

library(dplyr)
library(readr)
df %>% mutate(V1  = parse_number(V1))

data

df <- structure(list(V1 = c("region_24", "region_67", "region_30", 
"region_26", "region_29")), row.names = c(NA, -5L), class = "data.frame")
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213