Input
I have a dataframe as follows:
structure(list(DistalLESfromnarescm = c("31.9", "31.9", "33.1",
"33.3", "33.8", "34.0"), LESmidpointfromnarescm = c("31.2", "31.2",
"32.0", "32.0", "33.1", "33.2"), ProximalLESfromnarescm = c("30.1",
"30.1", "30.9", "30.9", "31.8", "31.9"), LESlengthcm = c("1.8",
"1.8", "2.2", "2.5", "2.0", "2.1"), EsophageallengthLESUEScenterscm = c("12.1",
"12.1", "14.0", "15.0", "15.1", NA), PIPfromnarescm = c("37.8",
"37.8", "No", "No", "34.3", "35.8"), Hosp_Id = c("A", "A", "B",
"B", "C", "D")), .Names = c("DistalLESfromnarescm", "LESmidpointfromnarescm",
"ProximalLESfromnarescm", "LESlengthcm", "EsophageallengthLESUEScenterscm",
"PIPfromnarescm", "Hosp_Id"), row.names = c(NA, -6L), class = "data.frame")
Aim
I would like to merge the value in any row with the preceding row if: a) The hospital number is the same and b) The value in that particular column between the grouped rows are not the same
The problem I have is how to lapply
within dplyr
because I don't know what to refer to in the left hand side of the lapply statement.
Attempt 1
result2 <- Question %>%
group_by(HospNum_Id,DistalLESfromnarescm)%>%
ifelse(HospNum_Id==lag(HospNum_Id),
lapply(WHAT DO I REFER TO HERE function(x) ifelse(x==lag(x), x,paste0(x,"::",lead(x)),"No")),"No")
Desired output
structure(list(DistalLESfromnarescm = c("31.9",
"33.1:33.3", "33.8", "34.0"), LESmidpointfromnarescm = c("31.2",
"32.0", "33.1", "33.2"), ProximalLESfromnarescm = c(
"30.1", "30.9", "31.8", "31.9"), LESlengthcm = c(
"1.8", "2.2:2.5", "2.0", "2.1"), EsophageallengthLESUEScenterscm = c(
"12.1", "14.0:15.0", "15.1", NA), PIPfromnarescm = c(
"37.8", "No", "34.3", "35.8"), Hosp_Id = c( "A",
"B", "C", "D")), .Names = c("DistalLESfromnarescm", "LESmidpointfromnarescm",
"ProximalLESfromnarescm", "LESlengthcm", "EsophageallengthLESUEScenterscm",
"PIPfromnarescm", "Hosp_Id"), row.names = c(NA, -4L), class = "data.frame")