how to convert a list to a number in R code example

Example: R convert list of lists into single numeric list

# 		Set seed so results able to be replicated:
set.seed(123)
# 		Make arbitrary List of sub-lists in each index:
list.of.lists <- list()
for (i in 1:3) {
  list.of.lists[[i]] <- rnorm(5)
}
# 		Print list.of.lists
list.of.lists
#[[1]]
#[1] -0.56047565 -0.23017749  1.55870831  0.07050839  0.12928774
#
#[[2]]
#[1]  1.7150650  0.4609162 -1.2650612 -0.6868529 -0.4456620
#
#[[3]]
#[1]  1.2240818  0.3598138  0.4007715  0.1106827 -0.5558411

# 		Convert to single list:
big.numeric <- as.numeric(do.call(c, list.of.lists))
big.numeric
#[1] -0.56047565 -0.23017749  1.55870831  0.07050839  0.12928774  1.71506499  0.46091621
#[8] -1.26506123 -0.68685285 -0.44566197  1.22408180  0.35981383  0.40077145  0.11068272
#[15] -0.55584113

Tags:

R Example