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

嵌套并调用tidyverse的map后,如何展开psych包输出的所有描述性统计结果

R语言psych包describe结果按分组列示解决方案

你可以通过调整数据分组逻辑与unnest规则,直接得到平铺的统计结果,调整后可运行代码如下:

library(tidyverse)
library(psych)

# 构造示例数据集
ds <- data.frame(quest = c(2,4,6,8), math_sum = rnorm(100,10,1), science_sum = rnorm(100,8,1))

# 版本1:得到所有分组的平铺描述统计结果(每一行对应一个quest+topic组合的所有统计量)
ds %>% 
  select(quest, ends_with("sum")) %>% 
  pivot_longer(-quest, names_to = "topic") %>% 
  group_by(quest, topic) %>% 
  summarise(stat = list(psych::describe(value) %>% as_tibble())) %>% 
  unnest(stat)

如果你需要和你期望输出一致的、按quest分组横向展示所有topic统计量的宽表效果,再增加一步宽表转换即可:

# 版本2:按quest分组横向展示所有topic的描述统计量
ds %>% 
  select(quest, ends_with("sum")) %>% 
  pivot_longer(-quest, names_to = "topic") %>% 
  group_by(quest, topic) %>% 
  summarise(stat = list(psych::describe(value) %>% as_tibble())) %>% 
  unnest(stat) %>% 
  pivot_wider(names_from = topic, values_from = vars:n)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.23 19:06:05