I have a line in following format
IP1: IP2: 0.1,0.5,0.9
I split this line using following command:
myVector <- (strsplit(oneLine, \":\"))
You can use gregexpr
and regmatches
to extract the numbers:
as.numeric(unlist(regmatches(oneLine, gregexpr("-?\\d+\\.\\d+", oneLine))))
# [1] 0.1 0.5 0.9
Here'a an approach using scan
:
oneLine <- "IP1: IP2: 0.1,0.5,0.9"
myVector <- strsplit(oneLine, ":")
listofPValues <- myVector[[1]][[3]]
listofPValues
# [1] " 0.1,0.5,0.9"
scan(text = listofPValues, sep = ",")
# Read 3 items
# [1] 0.1 0.5 0.9
And one using strsplit
:
as.numeric(unlist(strsplit(listofPValues, ",")))
# [1] 0.1 0.5 0.9