At first glance, I have a simple problem. I have a column with measurements that partly consist of one area. I.e:
a <- c("28", "", "5.6", "12-24")
Now I want to convert all values. I have chosen the following approach:
res <- ifelse(str_detect(a, "-"),
yes = paste(
as.character(as.double(unlist(str_extract_all(a, "[0-9.]+"))[1]) * 10),
"-",
as.character(as.double(unlist(str_extract_all(a, "[0-9.]+"))[2]) * 10)),
no = a)
The result looks like:
res
[1] "28" "" "5.6" "280 - 56"
The expected output is the following:
res
[1] "28" "" "5.6" "120 - 240"
Can someone explain to me why this is happening and what a correct approach can be?