Trying to coerce this vector as numeric:
vec <- c("10^2", "10^3", "10^6", "", "10^9")
vec <- as.numeric(vec)
[1] NA NA NA NA NA
Warning message:
NAs introduced by coercion
Output desired:
[1] "100" "1000" "1e+06" "" "1e+09"
split the strings at ^
and then coerce each part individually. We have to escape using \\
because ^
is a special regex character.
sapply(strsplit(vec, "\\^"), function(x){
as.numeric(x[1])^as.numeric(x[2])
})
#[1] 1e+02 1e+03 1e+06 NA 1e+09