You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在R中使用purrr::map()?基于鸢尾花数据集的分组统计实现

Solution Using purrr::map for Iris Dataset Statistics

Got it, let's walk through how to solve this problem exactly as you requested. We'll use purrr::map to process each species in the iris dataset, compute the max, mean, and min for the four measurement variables, and structure the output into a clean list.

Step 1: Split the Dataset by Species

First, we split the iris dataset into a list where each element is a subset of data for one species. This makes it easy to iterate over each group with map:

library(purrr)

# Split iris by Species into a list of data frames
iris_by_species <- split(iris, iris$Species)

Step 2: Define a Custom Statistic Calculation Function

Next, we create a function that takes a single species' data frame, extracts the species name, and computes the required stats for the four variables. We'll use dplyr here for concise code (but I'll also include a base R alternative below if you prefer):

library(dplyr)

calc_species_stats <- function(df) {
  # Get the unique species name for this subset
  species <- unique(df$Species)
  
  # Calculate max, mean, min for each measurement variable
  stats_df <- df %>%
    select(Sepal.Length, Sepal.Width, Petal.Length, Petal.Width) %>%
    summarise(across(everything(), list(max = max, mean = mean, min = min)))
  
  # Convert the stats data frame to a named vector for cleaner output
  stats_vector <- unlist(stats_df)
  
  # Return a list with species name and its stats
  list(species_name = species, statistics = stats_vector)
}

Step 3: Apply the Function with purrr::map

Now we use map to apply our function to each species subset. This will give us the structured list you need:

# Generate the final result
species_stats_list <- map(iris_by_species, calc_species_stats)

Step 4: Check the Output

If you print the first element of the result, you'll see it has the species name and a named vector of stats:

# View stats for setosa
species_stats_list[[1]]

Output will look like this:

$species_name
[1] "setosa"

$statistics
Sepal.Length_max  Sepal.Length_mean   Sepal.Length_min  Sepal.Width_max 
            5.8             5.006             4.3             4.4 
Sepal.Width_mean   Sepal.Width_min Petal.Length_max  Petal.Length_mean 
           3.428             2.3             1.9             1.462 
 Petal.Length_min  Petal.Width_max  Petal.Width_mean   Petal.Width_min 
            1.0             0.6             0.246             0.1 

Alternative: Base R Version (No dplyr)

If you don't want to use dplyr, here's a base R version of the calculation function:

calc_species_stats_base <- function(df) {
  species <- unique(df$Species)
  vars <- c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width")
  
  # Calculate stats for each variable using base R
  stats_list <- lapply(vars, function(var) {
    values <- df[[var]]
    c(max = max(values), mean = mean(values), min = min(values))
  })
  
  # Name the elements and convert to a single vector
  names(stats_list) <- vars
  stats_vector <- unlist(stats_list)
  
  list(species_name = species, statistics = stats_vector)
}

# Generate result with base R function
species_stats_list_base <- map(iris_by_species, calc_species_stats_base)

This approach gives you exactly what you asked for: a list where each element contains the species name (as a character string) and a named vector of max, mean, and min values for the four measurement variables.

内容的提问来源于stack exchange,提问作者CloverCeline

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:22:14