One day you may find it useful. It basically extarcts numeric values from a string. Let’s say your fieldwork/labwork assistant entered data - did great job but simply did not know that should’t mix characters with numeric.
To get what she/he did we need to do first the oposite;) So, let’s create x vector of 100-elements length, made of random letters. Then, create y vector of the same length but consisting of random numbers. Finally paste the two vectors to get what she/he actually created. Here is a piece of code for the above:
set.seed(44) # for repoducible result
x <- sample(LETTERS, 100, replace = TRUE)
y <- round(rnorm(100),2)
z <- paste(x, y, sep = "_")
So you have your variable, which looks like that (look at the first four elements):
## [1] "T_-2.63" "H_0.29" "N_-2.23" "O_-1.43"
…and you need to calculate a mean of it… You can not do it using the variable in its current form as it is not numeric! It will not work. You know it, your assistant did not…
The proof:
mean(z)
## Warning in mean.default(z): argument is not numeric or logical: returning
## NA
## [1] NA
Here is where the function of the day comes in handy. Watch R works miracle for you!!
mean(readr::parse_number(z)) # note you need readr package to be installed (do it before running this line otherwhise you will get no result)
## [1] -0.0957
End of hopefully instructive story! :)