I have a string like "[0 0.1 0.2 0.4]" and what I want is to strip parentheses and retrieve values as numeric. I am able to strip parentheses, but when it comes to convert to numeric, then I have an error with NA:
cleanList <- function(aString){
temp <- gsub("\\[|\\]", "", aString)
as.numeric(temp)
}
is there a way to convert each of the character in the string into numbers?
EDIT: here's another approach that uses stringr:
cleanList <- function(aString){
as.numeric(str_extract_all(aString,"\\(?[0-9,.]+\\)?")[[1]])
}
cleanList("[0 0.1 0.2 0.4]")
[1] 0.0 0.1 0.2 0.4