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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With