Этот вопрос основан на ответе на мой предыдущий вопрос a-specific-row-what-fulfi">здесь.
На самом деле у меня есть этот фрейм данных:
activity_type leg_mode route_distance
1 home access_walk 239.83275
2 pt interaction pt 15802.78756
3 pt interaction transit_walk 71.92245
4 pt interaction pt 2958.24598
5 pt interaction transit_walk 0.00000
6 pt interaction pt 9555.56836
Поскольку моя функция работает на векторной основе, я склеиваю столбцы вместе и работаю со следующим df, чтобы не потерять информацию:
activity_type__leg_mode__route_distance
1 home@[email protected]
2 pt interaction@[email protected]
3 pt interaction@[email protected]
4 pt interaction@[email protected]
5 pt interaction@transit_walk@0
6 pt interaction@[email protected]
Я пытаюсь применить эту строку кода к новому df:
r = rle(df$activity_type)
ix = c(
which(head(r$values, -1) == "pt interaction" & tail(r$values, -1) == "outside"), # p before o
which(head(r$values, -1) == "outside" & tail(r$values, -1) == "pt interaction") + 1) # o before p
Поэтому теперь мне нужна некоторая гибкость, так как новый df имеет не только pt interaction
или outside
, но и другие символы. Однако он должен проверять только начало строки. Я думал об использовании grep или strong, но я не уверен, как это сделать успешно.
В основном я хочу найти способ сделать это условие более гибким which(head(r$values, -1) == "pt interaction" & tail(r$values, -1) == "outside")
, т.е. оно должно искать не "pt interaction"
, а "pt interaction<some varying, but irrelevant stuff>"
.
Вот некоторые данные для вас, чтобы попробовать
c("home@[email protected]", "pt interaction@[email protected]",
"pt interaction@[email protected]", "pt interaction@[email protected]",
"pt interaction@transit_walk@0", "pt interaction@[email protected]",
"pt interaction@[email protected]", "outside@outside@0",
"outside@[email protected]", "outside@[email protected]",
"pt interaction@[email protected]", "pt interaction@transit_walk@0",
"pt interaction@[email protected]", "pt interaction@[email protected]",
"pt interaction@[email protected]", "pt interaction@[email protected]",
"home@[email protected]", "leisure@[email protected]",
"other@[email protected]", "leisure@[email protected]",
"leisure@[email protected]", "other@[email protected]",
"leisure@[email protected]", "other@[email protected]",
"leisure@[email protected]", "home@[email protected]",
"adpt interaction@adpt@NaN", "leisure@[email protected]",
"adpt interaction@[email protected]", "home@adpt@NaN", "@[email protected]",
"home@@NA", "outside@transit_walk@0", "outside@[email protected]",
"outside@[email protected]", "outside@[email protected]",
"pt interaction@[email protected]", "pt interaction@[email protected]",
"pt interaction@[email protected]", "pt interaction@[email protected]",
"outside@outside@0", "outside@[email protected]",
"outside@[email protected]", "pt interaction@[email protected]",
"pt interaction@[email protected]", "pt interaction@[email protected]",
"pt interaction@transit_walk@0", "pt interaction@[email protected]",
"pt interaction@[email protected]", "pt interaction@[email protected]",
"pt interaction@[email protected]", "outside@@NA",
"outside@[email protected]", "leisure@[email protected]",
"work@[email protected]", "outside@@NA", "outside@[email protected]",
"outside@[email protected]", "outside@[email protected]",
"leisure@[email protected]", "outside@@NA", "outside@[email protected]",
"pt interaction@[email protected]", "pt interaction@[email protected]",
"pt interaction@[email protected]", "pt interaction@[email protected]",
"outside@[email protected]", "pt interaction@[email protected]",
"pt interaction@[email protected]", "pt interaction@[email protected]",
"pt interaction@[email protected]", "pt interaction@[email protected]",
"work@[email protected]", "outside@@NA", "outside@[email protected]",
"other@[email protected]", "outside@@NA", "outside@[email protected]",
"pt interaction@[email protected]", "pt interaction@transit_walk@0",
"pt interaction@[email protected]", "pt interaction@[email protected]",
"outside@[email protected]", "pt interaction@[email protected]",
"pt interaction@transit_walk@0", "pt interaction@[email protected]",
"pt interaction@[email protected]", "work@[email protected]",
"pt interaction@[email protected]", "pt interaction@[email protected]",
"outside@@NA", "outside@[email protected]", "pt interaction@[email protected]",
"pt interaction@transit_walk@0", "pt interaction@[email protected]",
"pt interaction@[email protected]", "outside@[email protected]",
"pt interaction@[email protected]", "pt interaction@[email protected]",
"pt interaction@[email protected]")