Home > Mobile >  search a pattern in column and only keep the pattern in R
search a pattern in column and only keep the pattern in R

Time:09-25

I have a column called fruits i.e "apples [8]; bananas [102], pineapple [4000], total" I only want to grade the string that starts with [...] and create a new column called code. So my new column would be [8], [102], [4000], AND THE last row would be na if there is no [..]

My code seems to be doing the reverse noc$CODE <- gsub("\\s\\[.*\\]","[.*\\]",noc$Name)

CodePudding user response:

If you want the brackets in your vector/df, then

 gsub("[\\[\\]]", "", regmatches(vec, gregexpr("\\[.*?\\]", vec)))
[1] "[8]"          "[102]"        "[4000]"       "character(0)"

and if you don't want the brackets:

unlist(str_match_all(vec, "(?<=\\[). ?(?=\\])"))
[1] "8"    "102"  "4000"

EDIT: Based on Question

v <- data.frame(vec) %>% 
mutate(bracketed = gsub("[\\[\\]]", "", regmatches(vec, gregexpr("\\[.*?\\]", vec))), 
       bracketed_adj = if_else(bracketed == "character(0)", '[99999]', bracketed))

I would do it after that way it doesn't interfere or do anything hinky (technical term) to the output.

  •  Tags:  
  • r
  • Related