Workspace
Nindiri Armenta/

RDocumentation: map

0
Beta
Spinner

Note that this notebook was automatically generated from an RDocumentation page. It depends on the package and the example code whether this code will run without errors. You may need to edit the code to make things work.

install.packages("GA", repos = "http://cran.us.r-project.org",
                 type = "source", dependencies = TRUE)
if(!require('purrr')) {
    install.packages('purrr')
    library('purrr')
}
1:10 %>%
  map(rnorm, n = 10) %>%
  map_dbl(mean)

# Or use an anonymous function
1:10 %>%
  map(function(x) rnorm(10, x))

# Or a formula
1:10 %>%
  map(~ rnorm(10, .x))

# Extract by name or position
# .default specifies value for elements that are missing or NULL
l1 <- list(list(a = 1L), list(a = NULL, b = 2L), list(b = 3L))
l1 %>% map("a", .default = "???")
l1 %>% map_int("b", .default = NA)
l1 %>% map_int(2, .default = NA)

# Supply multiple values to index deeply into a list
l2 <- list(
  list(num = 1:3,     letters[1:3]),
  list(num = 101:103, letters[4:6]),
  list()
)
l2 %>% map(c(2, 2))

# Use a list to build an extractor that mixes numeric indices and names,
# and .default to provide a default value if the element does not exist
l2 %>% map(list("num", 3))
l2 %>% map_int(list("num", 3), .default = NA)

# A more realistic example: split a data frame into pieces, fit a
# model to each piece, summarise and extract R^2
mtcars %>%
  split(.$cyl) %>%
  map(~ lm(mpg ~ wt, data = .x)) %>%
  map(summary) %>%
  map_dbl("r.squared")

# Use map_lgl(), map_dbl(), etc to reduce to a vector.
# * list
mtcars %>% map(sum)
# * vector
mtcars %>% map_dbl(sum)

# If each element of the output is a data frame, use
# map_dfr to row-bind them together:
mtcars %>%
  split(.$cyl) %>%
  map(~ lm(mpg ~ wt, data = .x)) %>%
  map_dfr(~ as.data.frame(t(as.matrix(coef(.)))))
# (if you also want to preserve the variable names see
# the broom package)
# Supply multiple values to index deeply into a list
l2 <- list(
  list(num = 1:3,     letters[1:3]),
  list(num = 101:103, letters[4:6]),
  list()
)
l2[2]
l2 %>% map(c(2, 2))
formula <- "y ~ b0"
for(i in 1:100) {
  formula <- paste(formula, "+ b", i, "* x", i, sep="")
}
formula
library(GA)
library(GA)

# Definir función de fitness
fitness <- function(x) {
  # Evaluar polinomio en valores de x
  fo <- sum(x * c(0.2706, 0.3456, 0.1121, 0.2706))
  
  # Retornar valor objetivo (negativo del polinomio, ya que se busca maximizar)
  return(-fo)
}


# Definir límites para los valores de x
lower <- rep(0, 4)
upper <- rep(25, 4)

# Ejecutar algoritmo genético para optimizar la función de fitness
resultados <- ga(type = "real-valued", fitness = fitness, lower = lower, upper = upper, maxiter = 100)
summary(resultados)
library(GA)

# Definir función de fitness
fitness <- function(x) {
  # Evaluar polinomio en valores de x
  y <- b0 + sum(x * b[1:100])
  
  # Retornar valor objetivo (negativo del polinomio, ya que se busca maximizar)
  return(-y)
}

# Definir coeficientes del polinomio
b0 <- 1
b <- runif(100, -1, 1)  # coeficientes aleatorios entre -1 y 1

# Definir límites para los valores de x
lower <- rep(-10, 100)
upper <- rep(10, 100)

# Ejecutar algoritmo genético para optimizar la función de fitness
resultados <- ga(type = "real-valued", fitness = fitness, lower = lower, upper = upper, maxiter = 100)
summary(resultados)
  • AI Chat
  • Code