Мы могли бы использовать str_locate
library(tidyverse)
df %>%
mutate(position = str_locate_all(String, "B") %>%
map(~ .x[,1])) %>%
unnest
# String start score position
#1 TRIRXBGFPI 219 46.1 6
#2 QBPWJOTFLQ 430 21.5 2
#3 ZDLYRUTZBB 380 32.1 9
#4 ZDLYRUTZBB 380 32.1 10
Или использовать gregexpr
из base R
lst <- lapply(gregexpr("B", df$String), function(x) as.numeric(x * NA^(x < 0)))
# or use strsplit to split the string and then get the index with which
#lst <- lapply(strsplit(df$String, ""), function(x) {
# x1 <- which(x == "B")
# if(length(x1) == 0) NA else x1})
out <- df[rep(seq_len(nrow(df)), lengths(lst)),]
out$position <- unlist(lst)
out1 <- out[!is.na(out$position),]
row.names(out1) <- NULL
out1
# String start score position
#1 TRIRXBGFPI 219 46.1 6
#2 QBPWJOTFLQ 430 21.5 2
#3 ZDLYRUTZBB 380 32.1 9
#4 ZDLYRUTZBB 380 32.1 10
данные
df <- structure(list(String = c("TRIRXBGFPI", "QBPWJOTFLQ", "PWVEEHKTFW",
"AWGAFAHGQF", "ZDLYRUTZBB"), start = c(219L, 430L, 399L, 246L,
380L), score = c(46.1, 21.5, 37.2, 16.4, 32.1)), class = "data.frame",
row.names = c(NA, -5L))