Let's say I have a dataset that looks like the following, and then, I proceed to group the rows by arranging them using NU_DN and DATE_HOSP:
NU_DN <- c("55284765", "55293825" , "55284765", "55293825", "55318351", "55293825")
DATE_HOSP <- c("2012-05-08", "2012-06-17", "2012-09-13", "2012-01-13", "2012-01-10", "2012-05-04" )
MOTIV <- c("2.1 - Reason 1", "1.2 - Reason 3", "6.7 - Reason 8", "2.2 - Reason 2", "3.3 - Reason 4", "6.9 - Reason 10")
data <- as.data.frame(cbind(NU_DN, DATE_HOSP, MOTIV))
data <- data %>%
arrange(NU_DN, DATE_HOSP)%>%
type.convert(as.is = TRUE)%>%
group_by(NU_DN) %>%
ungroup()
After that's done, I get the following dataframe:
NU_DN DATE_HOSP MOTIV
55284765 2012-05-08 2.1 - Reason 1
55284765 2012-09-13 6.7 - Reason 8
55293825 2012-01-13 2.2 - Reason 2
55293825 2012-05-04 6.9 - Reason 10
55293825 2012-06-17 1.2 - Reason 3
55318351 2012-01-10 3.3 - Reason 4
What I want to do is to select the following row of the cluster if "MOTIV" starts with either 2 or 3. So if MOTIV for for NU_DN 55293825 is "2.2 - Reason 2", I want to select the next row that it's MOTIV is '6.9 - Reason 10" AND "1.2 - Reason 3". My desired output is the following:
NU_DN DATE_HOSP MOTIV
55284765 2012-09-13 6.7 - Reason 8
55293825 2012-05-04 6.9 - Reason 10
55293825 2012-06-17 1.2 - Reason 3
Is there any way I can do that?
CodePudding user response:
How about removing the lines that start with either 2 or 3?
str_detect()
allows detecting the presence of strings that start with 2 (^2
) or 3 (^3
).
negate = TRUE
returns non-matching elements.
library(tidyverse)
data %>%
dplyr::filter(str_detect(MOTIV, c("^2", "^3"), negate = TRUE))
CodePudding user response:
If you want all non-matched rows:
data %>% filter(!grepl("^3|^2", MOTIV))
1 55284765 2012-09-13 6.7 - Reason 8
2 55293825 2012-05-04 6.9 - Reason 10
3 55293825 2012-06-17 1.2 - Reason 3
If you only want the one row that follows any match (it assumes it will also not be a match):
idx <- data %>%
mutate(i=grepl("^3|^2", MOTIV)) %>%
pull(i)
data[which(idx) 1,] %>%
drop_na()
1 55284765 2012-09-13 6.7 - Reason 8
2 55293825 2012-05-04 6.9 - Reason 10