Suppose I have a character variable normal(0,1). I want to create 3 variables out of it:
distribution <- "normal"
mu <- 0
sd <- 1
I can do this with stringr but i'm wondering if there is a better way of writing this code
library(stringr)
distribution_chr <- "normal(0,1)"
distribution <- str_split(string = distribution_chr, pattern = "\\(", simplify = TRUE)[1,1]
parameters <- distribution_chr %>%
str_extract_all("(?<=\\().+?(?=\\))") %>%
str_split(pattern = ",", simplify = TRUE)
mu <- as.numeric(parameters[1,1])
sd <- as.numeric(parameters[1,2])
Created on 2019-07-20 by the reprex package (v0.2.1)
Thanks!