Если вы хотите посчитать, сколько цифр 6-ди git появляется в каждом элементе столбца case.detail
, то делайте (в базе R):
library(stringr)
df$count = sapply( str_match_all( df$case.details, "\\b\\d{6}\\b"), length)
Пример:
df = structure(list(name = c("A3 ", "B5 ", "C8 "),
sex = c(" M", " male", " F "),
case.details = c(" 020319 entry, 030419 exit ", " 040819 in and 050819 out ", " 081119 in "),
type = c(" second gen AB-1", " second gen AB-2"," AB-1/2, (second gen)")), class = "data.frame", row.names = c(NA,
df$count = sapply(str_match_all(df$case.details, "\\b\\d{6}\\b"), length)
Результат:
> df
name sex case.details type count
1 A3 M 020319 entry, 030419 exit second gen AB-1 2
2 B5 male 040819 in and 050819 out second gen AB-2 2
3 C8 F 081119 in AB-1/2, (second gen) 1