I have a variable called summed_EU_Sales, I want to get EU only.
I have tried to do it using gsub this way:
gsub(".*summed_", "", summed_EU_Sales)
which only removes the first summed_. I struggle to remove both of the sides.
I have a variable called summed_EU_Sales, I want to get EU only.
I have tried to do it using gsub this way:
gsub(".*summed_", "", summed_EU_Sales)
which only removes the first summed_. I struggle to remove both of the sides.
Assuming your variable name summed_EU_Sales is actually a string "summed_EU_Sales", we can use regex groupings:
sub(".*_(EU)_.*", "\\1", "summed_EU_Sales")
#> [1] "EU"
Created on 2022-12-19 by the reprex package (v2.0.1)
library(stringr)
library(tidyverse)
x<-cbind.data.frame("summed_EU_Sales"=c(1:5),
"summed_jap_Sales"=c(1:5))
names(x) <- str_match(names(x), "_\\s*(.*?)\\s*_")[,2]