How would I combine any row with a single column entry into a single combined input in a new column? e.g. when column A has value, but B-C are empty, I would like to merge the row entries into a single input in column D.
original file is a txt file that looks like this:
A|B|C
1|2|3
1
text
2
[end]
4|5|6
2
1
[end]
df <-read.delim("file.txt", header=TRUE, sep="|", blank.lines.skip = TRUE)
A B C
1 2 3
1
text
2
[end]
4 5 6
2
1
[end]
desired out data table with newly added column D:
A B C D
1 2 3 1 text 2 [end]
4 5 6 2 1 [end]
I imagine this would be combination of is.na and mutate functions but have been unable to find a solution. The code could also include ends_with("[end]") since each row that I want to combine ends with this text. Any thoughts on this?