I have a process that does a few checks to a data frame, and at each check if the check passes I want to add a text to a column with a separator. So suppose after the first test rows 2 and 3 pass, so the msg column has "first" in it. Then the second test updated the ok column and is true for rows 1 and 2, giving the following:
> d = data.frame(ok=c(TRUE,TRUE,FALSE,FALSE), msg=c("", "first","first",""))
> d
ok msg
1 TRUE
2 TRUE first
3 FALSE first
4 FALSE
so the next step would be to add "second" to the msg column in rows 1 and 2 only, resulting in:
ok msg
1 TRUE second
2 TRUE first;second
3 FALSE first
4 FALSE
I can't work out how to do it. This first effort leaves a leading separator in the initial case:
> paste(d$msg[d$ok],"second", sep=";")
[1] ";second" "first;second"
This returns a length-3 vector which is clearly wrong:
> paste(c(d$msg[d$ok],"second"), sep=";")
[1] "" "first" "second"
and anything with collapse returns a length-1 vector which is also wrong.
Sledgehammer solution is to use the first effort above and then strip any leading separators at the end, but that's ugly. I'm hoping for something neater.
Solutions should only use base R functions, and the initial "empty string" doesn't have to be "" - but I've played with NA and got nowhere. A solution neater (in my opinion) than my sledgehammer will get accepted.