suresh
suresh

Reputation: 33

Multiple search in a record

I have a variable code in a dataset sample

code
asdas%4shdgjas%4hj%4
asdsadsad
fgfd%4akk

I want to process in such a way that when it find %4 in the variable it should assign the position of the %4 to the variable pos(for example in first records it comes at 6).after that it need to create another variable val such that it should substring the variable code from the value of pos.

Upvotes: 0

Views: 35

Answers (1)

lukeA
lukeA

Reputation: 54237

You could do

df <- data.frame(code=c("asdas%4shdgjas%4hj%4", "asdsadsad", "fgfd%4akk"), stringsAsFactors = FALSE)
df$pos <- sapply(gregexpr("%4", df$code, fixed = TRUE), "[", 1)
df$val <- with(df, substr(code, pos, nchar(code)))
df
#                   code pos             val
# 1 asdas%4shdgjas%4hj%4   6 %4shdgjas%4hj%4
# 2            asdsadsad  -1       asdsadsad
# 3            fgfd%4akk   5           %4akk

Here's an alternative for multiple matches, which may need install.packages("tidyr") before:

df <- data.frame(code=c("asdas%4shdgjas%4hj%4", "asdsadsad", "fgfd%4akk"), stringsAsFactors = FALSE)
df$pos <- gregexpr("%4", df$code, fixed = TRUE)
df <- tidyr::unnest(df)
df$val <- with(df, substr(code, pos, nchar(code)))
df
# Source: local data frame [5 x 3]
# 
#                   code   pos             val
#                  (chr) (int)           (chr)
# 1 asdas%4shdgjas%4hj%4     6 %4shdgjas%4hj%4
# 2 asdas%4shdgjas%4hj%4    15          %4hj%4
# 3 asdas%4shdgjas%4hj%4    19              %4
# 4            asdsadsad    -1       asdsadsad
# 5            fgfd%4akk     5           %4akk

Upvotes: 1

Related Questions