Я пытаюсь отфильтровать фрейм данных с помощью dplyr по группам, где фиксируется первое вхождение строки 'ReadingOnset' в строке, и оно и все последующие строки передаются в новый фрейм данных.
Text_Stimuli <- structure(list(Name = c("Sub1", "Sub1", "Sub1", "Sub1", "Sub1",
"Sub1", "Sub1", "Sub1", "Sub1", "Sub1", "Sub1", "Sub1", "Sub1",
"Sub1", "Sub1", "Sub1", "Sub1", "Sub1", "Sub1", "Sub1", "Sub1",
"Sub1", "Sub1", "Sub1", "Sub1", "Sub1", "Sub1", "Sub1", "Sub1",
"Sub1", "Sub1", "Sub1", "Sub1", "Sub1", "Sub1", "Sub1", "Sub1",
"Sub1", "Sub1", "Sub1", "Sub1", "Sub1", "Sub1", "Sub1", "Sub1",
"Sub1", "Sub1", "Sub1", "Sub1", "Sub1", "Sub1", "Sub1", "Sub1",
"Sub1", "Sub1", "Sub1", "Sub1", "Sub1", "Sub1", "Sub1", "Sub1",
"Sub1", "Sub1", "Sub1", "Sub1", "Sub1", "Sub1", "Sub1", "Sub1",
"Sub1", "Sub1", "Sub1", "Sub1", "Sub1", "Sub1", "Sub1", "Sub1",
"Sub1", "Sub1", "Sub1", "Sub1", "Sub1", "Sub1", "Sub1", "Sub1",
"Sub1", "Sub1", "Sub1", "Sub1", "Sub1", "Sub1", "Sub1", "Sub1",
"Sub1", "Sub1", "Sub1", "Sub1", "Sub1", "Sub1", "Sub1", "Sub1"
), StimulusName = c("GenLie20", "GenLie20", "GenLie20", "GenLie20",
"GenLie20", "GenLie20", "GenLie20", "GenLie20", "GenLie20", "GenLie20",
"GenLie20", "GenLie20", "GenLie20", "GenLie20", "GenLie20", "GenLie20",
"GenLie20", "GenLie20", "GenLie20", "GenLie20", "GenLie20", "GenLie20",
"GenLie20", "GenLie20", "GenLie20", "GenLie20", "GenLie20", "GenLie20",
"GenLie20", "GenLie20", "GenLie20", "GenLie20", "GenLie20", "GenLie20",
"GenLie20", "GenLie20", "GenLie20", "GenLie20", "GenLie20", "GenLie20",
"GenLie20", "GenLie20", "GenLie20", "GenLie20", "GenLie20", "GenLie20",
"GenLie20", "GenLie20", "GenLie20", "GenLie20", "GenLie20", "GenLie20",
"GenLie20", "GenLie20", "GenLie20", "GenLie20", "GenLie20", "GenLie20",
"GenLie20", "GenLie20", "GenLie20", "GenLie20", "GenLie20", "GenLie20",
"GenLie20", "GenLie20", "GenLie20", "GenLie20", "GenLie20", "GenLie20",
"GenLie20", "GenLie20", "GenLie20", "GenLie20", "GenLie20", "GenLie20",
"GenLie20", "GenLie20", "GenLie20", "GenLie20", "GenLie20", "GenLie20",
"GenLie20", "GenLie20", "GenLie20", "GenLie20", "GenLie20", "GenLie20",
"GenLie20", "GenLie20", "GenLie20", "GenLie20", "GenLie20", "GenLie20",
"GenLie20", "GenLie20", "GenLie20", "GenLie20", "GenLie20", "GenLie20",
"GenLie20"), StimuliBlock = c("Block_4", "Block_4", "Block_4",
"Block_4", "Block_4", "Block_4", "Block_4", "Block_4", "Block_4",
"Block_4", "Block_4", "Block_4", "Block_4", "Block_4", "Block_4",
"Block_4", "Block_4", "Block_4", "Block_4", "Block_4", "Block_4",
"Block_4", "Block_4", "Block_4", "Block_4", "Block_4", "Block_4",
"Block_4", "Block_4", "Block_4", "Block_4", "Block_4", "Block_4",
"Block_4", "Block_4", "Block_4", "Block_4", "Block_4", "Block_4",
"Block_4", "Block_4", "Block_4", "Block_4", "Block_4", "Block_4",
"Block_4", "Block_4", "Block_4", "Block_4", "Block_4", "Block_4",
"Block_4", "Block_4", "Block_4", "Block_4", "Block_4", "Block_4",
"Block_4", "Block_4", "Block_4", "Block_4", "Block_4", "Block_4",
"Block_4", "Block_4", "Block_4", "Block_4", "Block_4", "Block_4",
"Block_4", "Block_4", "Block_4", "Block_4", "Block_4", "Block_4",
"Block_4", "Block_4", "Block_4", "Block_4", "Block_4", "Block_4",
"Block_4", "Block_4", "Block_4", "Block_4", "Block_4", "Block_4",
"Block_4", "Block_4", "Block_4", "Block_4", "Block_4", "Block_4",
"Block_4", "Block_4", "Block_4", "Block_4", "Block_4", "Block_4",
"Block_4", "Block_4"), Reading_Onset = c("", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "ReadingOnset", "", "", "", "",
"", "", "")), row.names = c(NA, -101L), vars = c("Name", "StimulusName",
"StimuliBlock"), drop = TRUE, indices = list(0:100), group_sizes = 101L, biggest_group_size = 101L, labels = structure(list(
Name = "Innocent Subject 15", StimulusName = "GenLie20",
StimuliBlock = "Block_4"), row.names = c(NA, -1L), class = "data.frame", vars = c("Name",
"StimulusName", "StimuliBlock"), drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
Вот пример типа решения, которое я пытался заставить работать, но безуспешно.
Test <- Text_Stimuli %>%
group_by(Name, StimulusName, StimuliBlock)%>%
filter(!lead(cumsum(grepl("ReadingOnset", Reading_Onset)), default = 0))
Как видите, я пытаюсь сгруппировать по Name
StimulusName
и StimuliBlock
.Затем я пытаюсь найти первое вхождение 'ReadingOnset' в столбце Reading_Onset
и вернуть все ведущие строки из этого (включая строку с 'ReadingOnset').
Я пыталсяприспособить это решение к обратной задаче: https://stackoverflow.com/a/37922522/2653210