Simon
Simon

Reputation: 59

Select list-element with special character

I am trying to subset a list, based on special characters attached to the elements. I found multiple results, but I think I lack the understand on how to deal with special characters and find elements containing special characters. I would like to have a result with all the elements containing '**' at the end. I appreciate any help and I apologize if there was a similar question already, which I did not found. A general answer on dealing with special characters when subsetting a list will also be much appreciated. Thank you for your time.

Example-Code:

x1 <- c('a**', 'b', 'c**')
x2 <- c('d**', 'b', 'e**')

y <- list(x1,x2)

#Here are a couple of results i tried:

grep('\\**', y, value =TRUE, ignore.case = TRUE)

y[lapply(y, function(x) x[grep("[^\\**]", x),])]

Filter(function(x) !any(grepl("[^\\**]", x)), y)

y[y = "[^\\**]"]

Upvotes: 1

Views: 508

Answers (1)

akrun
akrun

Reputation: 887501

We can use grep with fixed=TRUE

grep("**", unlist(y), value = TRUE, fixed = TRUE)
#[1] "a**" "c**" "d**" "e**"

Upvotes: 1

Related Questions