With a regular expression and the function gsub():
group <- c("12357e", "12575e", "197e18", "e18947")
group
[1] "12357e" "12575e" "197e18" "e18947"
gsub("e", "", group)
[1] "12357" "12575" "19718" "18947"
What gsub does here is to replace each occurrence of "e" with an empty string "".
See ?regexp or gsub for more help.
Str_replace_all - how to use
Str_replace Regex in R - Stack Overflow
regex - Trying to replace a () in a string in R using str_replace - Stack Overflow
Replace String - Looping Over Vector Help
With a regular expression and the function gsub():
group <- c("12357e", "12575e", "197e18", "e18947")
group
[1] "12357e" "12575e" "197e18" "e18947"
gsub("e", "", group)
[1] "12357" "12575" "19718" "18947"
What gsub does here is to replace each occurrence of "e" with an empty string "".
See ?regexp or gsub for more help.
Regular expressions are your friends:
R> ## also adds missing ')' and sets column name
R> group<-data.frame(group=c("12357e", "12575e", "197e18", "e18947")) )
R> group
group
1 12357e
2 12575e
3 197e18
4 e18947
Now use gsub() with the simplest possible replacement pattern: empty string:
R> group$groupNoE <- gsub("e", "", group$group)
R> group
group groupNoE
1 12357e 12357
2 12575e 12575
3 197e18 19718
4 e18947 18947
R>
You can use str_replace_all and add another \ when escaping \ in the regex.
library(tidyverse)
Example %>% mutate(Column1 = str_replace_all(Column1, "\\([^()]*\\)", ""))
# Column1
#1 Pineapple
#2 Roger
#3
#4 First , Second
It's not clear to me how you want to deal with entries where you have more than one number. That aside and generally, a more convenient option is to use readr::parse_number, rather than using stringr::str_detect/stringr::str_remove. parse_number takes care of additional text, units and thousands separators.
If you want to keep only the first number (in the cases where there are more than one number per entry), you can do
library(tidyverse)
Example %>% mutate(Column1 = parse_number(Column1))
# Column1
#1 1000
#2 50000
#3 1000
#4 100
Or if you want to keep both/multiple numbers, I suggest using separate_rows to separate entries based on a comma followed by a whitespace, before using readr::parse_number.
Example %>%
separate_rows(Column1, sep = ",\\s") %>%
mutate(Column1 = parse_number(Column1))
## A tibble: 5 × 1
# Column1
# <dbl>
#1 1000
#2 50000
#3 1000
#4 100
#5 1000
Update
To separate keys and values, here is an option; please see inline comments for explanations:
library(tidyverse)
Example %>%
# Separate multiple comma-separated entries into rows
separate_rows(Column1, sep = ",\\s") %>%
# Swap "(value) key" > "key (value)" %>%
mutate(Column1 = str_replace(
Column1, "^(\\(.+\\))\\s(\\w+)$", "\\2 \\1")) %>%
# Separate "key (value)" into columns
separate(Column1, c("key", "value"), sep = "\\s", fill = "left") %>%
# Parse number
mutate(value = parse_number(value))
## A tibble: 5 × 2
# key value
# <chr> <dbl>
#1 Pineapple 1000
#2 Roger 50000
#3 NA 1000
#4 First 100
#5 Second 1000
Sample data
Example <- data.frame(Column1 = c(
"Pineapple (
50,000) Roger",
"($1,000)",
"First ($100), Second ($1,000)"))
Hello,
I am needing some help with replacing a character in each value in my vector. This is my vector.
years <- c("X1996","X1997","X1998","X1999","X2000","X2001","X2002","X2003","X2004","X2005","X2006","X2007","X2008","X2009","X2010","X2011","X2012","X2013","X2014","X2015")What I am trying to do is replace the "X" in each item in the vector. When I do it individually I get the results I want:
y <- years[1] y #[1] "X1996" y <- gsub(pattern = "X",replacement = "",x=y) y #[1] "1996"
When I loop over it, i just get the numbers 1:20:
for(i in 1:length(years)){
years[i] <- gsub(pattern = "X",replacement = "",x=i)
}
years
#[1] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "12" "13" "14" "15" "16" "17" "18" "19" "20"What am I doing wrong? I have worked with vectors before but can't seem to figure out what is happening.