I have a dataframe in r with two fields containing a list of strength assessments and another with a list of date/times. I want to obtain the index from the strength list of the first occurrence of the pattern, "Strong" and use that index to select the corresponding date/time, performing this process for each record.
# Data frame: determine time when strength returns.
# Serial strength assessments, date-time values for each assessment Some missing values. Some not returning to "Strong"
Strength <- c(NA,"Strong; Strong","Unable to assess; Moderate; Moderate; Moderate; Moderate; Moderate; Strong","Moderate; Moderate; Moderate; Moderate; Strong; Strong","Moderate; Strong; Strong; Moderate; Moderate", "Moderate")
Time <- c(NA,"2021-04-16 23:50; 2021-04-17 08:35","2021-02-04 13:00; 2021-02-04 14:45; 2021-02-04 16:59; 2021-02-04 19:27; 2021-02-05 08:20; 2021-02-05 20:18; 2021-02-06 11:00","2021-01-23 08:25; 2021-01-23 19:49; 2021-01-24 08:08; 2021-01-24 19:56; 2021-01-25 19:40; 2021-01-26 09:00","2021-01-14 04:46; 2021-01-14 07:33; 2021-01-14 19:47; 2021-01-15 08:48","2021-01-26 09:00")
ST <- as.data.frame(cbind(Strength,Time))
view(ST)
# Create lists: separate
s.list <- lapply(strsplit(ST$Strength, ";"),unlist)
t.list <- lapply(strsplit(ST$Time, ";"),unlist)
# Determine index of first instance of "Strong" in s.list and apply to t.list (time)
ST$time <-mapply(function(x,y) {x[which.max(y==c("Strong"))]}, t.list,s.list)
view(ST
This code seems to select only the first time for each time list. Also places "character(0)" where I would like a "NA".
base R