Home > Blockchain >  How to get the average from an array like string in R?
How to get the average from an array like string in R?

Time:11-21

I have this string variable.

x <- "[2,3,3,5]"

I want to get the average of this. How can I achieve this on R?

CodePudding user response:

In base R:

mean(as.numeric(strsplit(x, '\\D')[[1]]), na.rm = TRUE)
#> [1] 3.25

CodePudding user response:

Extract all the digits and then take the mean:

library(stringr)

mean(as.numeric(str_extract_all(x, "[0-9]")[[1]]))
[1] 3.25

CodePudding user response:

Looks like json format.

mean(jsonlite::fromJSON(x))
# [1] 3.25

Data:

x <- "[2,3,3,5]"

CodePudding user response:

You can also change the "["'s into "("'s and ask for R to interpret the string as a expression with parse and eval:

stringr::str_replace_all(x, c("\\[" = "c\\(", "\\]" = "\\)")) %>% parse(text = .) %>% eval() %>% mean()

CodePudding user response:

We can replace [] as c() to make a valid expression string in R and then eval it, e.g.,

> mean(eval(str2lang(paste0("c", chartr("[]", "()", x)))))
[1] 3.25

or using scan substr

> mean(scan(text = substr(x, 2, nchar(x) - 1), sep = ",", quiet = TRUE))
[1] 3.25

Or, Similarily, we can try py_eval to parse the expression in a Python manner

> library(reticulate)

> mean(py_eval(x))
[1] 3.25
  • Related