1) Assuming that only the forms shown in the question are allowed replace colons with minus signs and then replace minus followed by space with space, minus, space.
library(sqldf)
sqldf("select *, replace(replace([Col1], ':', '-'), '- ', ' - ') as New from df")
giving:
Col1 Col2 Col3 New
1 ten: end 5 10 ten - end
2 five: nb 7 11 five - nb
3 12:4 12 10 12-4
4 13:56 15 16 13-56
2) If we can assume that the only forms are number:number or character: character and that the second form contains no digits.
sqldf("select *,
case when strFilter(Col1, '0123456789') = ''
then replace(Col1, ':', ' -')
else replace(Col1, ':', '-')
end as New
from df")
giving:
Col1 Col2 Col3 New
1 ten: end 5 10 ten - end
2 five: nb 7 11 five - nb
3 12:4 12 10 12-4
4 13:56 15 16 13-56
3) This first checks for numbers:numbers and then checks for characters: characters where characters can only be numbers or lower case letters.
dig <- "0123456789"
diglet <- "0123456789abcdefghijklmnopqrstuvwxyz"
fn$sqldf("select *,
case when trim(Col1, '$dig') = ':'
then replace(Col1, ':', '-')
when trim(Col1, '$diglet') = ': '
then replace(Col1, ': ', ' - ')
else Col1 end as New
from df")
giving:
Col1 Col2 Col3 New
1 ten: end 5 10 ten - end
2 five: nb 7 11 five - nb
3 12:4 12 10 12-4
4 13:56 15 16 13-56
4) This one extracts the x:y and checks whether x and y are number and if so does the appropriate replacement and if no match it extracts x:yz where y is a space and if x and z are digits or lower case then it performs the appropriate replacement and otherwise returns Col1. dig
and diglet
are from above.
fn$sqldf("select *,
case when trim(substr(Col1, instr(Col1, ':')-1, 3), '$dig') = ':'
then replace(Col1, ':', '-')
when trim(substr(Col1, instr(Col1, ':') -1, 4), '$diglet') = ': '
then replace(Col1, ': ', ' - ')
else Col1 end as New
from df")
Note
The input in reproducible form is:
Lines <- "Col1,Col2,Col3
ten: end,5,10
five: nb,7,11
12:4,12,10
13:56,15,16"
df <- read.csv(text = Lines, as.is = TRUE, strip.white = TRUE)