3
votes

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, ":")) ,where oneline is string "IP1: IP2: 0.1,0.5,0.9"

I want to access the third column and convert it into data frame. I access the third column as below: listofPValues <- myVector[[1]][[3]]

listofPValues <- myVector[[1]][[3]]

I am trying to convert the comma separated values in listofPValues to dataframe as below:

data.frame(as.numeric(listofPValues,',')))

It gives me following error

In data.frame(as.numeric(listofPValues, ",")) :
 NAs introduced by coercion

The function that I want to use accepts only data frames. I guess the problem is I need to break the string "0.1,0.5,0.9" before I convert it to numeric. Please let me know if you have any suggestion.

Thanks tictactoe

3
Try scan or strsplit again. - A5C1D2H2I1M1N2O1R2T1

3 Answers

8
votes

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
3
votes

You can use gregexpr and regmatchesto extract the numbers:

as.numeric(unlist(regmatches(oneLine, gregexpr("-?\\d+\\.\\d+", oneLine))))
# [1] 0.1 0.5 0.9
0
votes

We can use str_extract

library(stringr)
as.numeric( str_extract_all(oneLine, "\\b[0-9.]+\\b")[[1]])
[1] 0.1 0.5 0.9

data

oneLine <- "IP1: IP2: 0.1,0.5,0.9"