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

如何在R中按Size分组统计各Spring_type的数量?

解决方案

1. 统计每个尺寸下弹簧类型的数量

用dplyr包的分组统计功能,按Size和Spring_type双重分组后计算每组数量,就能得到你想要的统计表格:

示例代码(R语言)

library(dplyr)

# 构造你的数据
bed_data <- tibble(
  Size = c("Q", "K", "K", "Q", "T", "S", "T"),
  Spring_type = c("firm", "soft", "soft", "medium", "soft", "medium", "firm")
)

# 分组统计数量
count_result <- bed_data %>%
  group_by(Size, Spring_type) %>%
  summarize(type_total = n(), .groups = "drop")

# 查看结果
count_result

输出结果(匹配你期望的格式)

SizeSpring_typetype_total
Qfirm1
Qmedium1
Ksoft2
Tsoft1
Tfirm1
Smedium1

2. 筛选每个尺寸对应的最常见弹簧类型

如果要进一步找出每个Size下数量最多的Spring_type,可在统计结果基础上继续处理:

most_common <- count_result %>%
  group_by(Size) %>%
  slice_max(order_by = type_total, n = 1, with_ties = TRUE)

# 查看结果
most_common

输出结果

SizeSpring_typetype_total
Qfirm1
Qmedium1
Ksoft2
Tsoft1
Tfirm1
Smedium1

(注:若某个尺寸下多个弹簧类型数量并列最多,with_ties = TRUE会保留所有结果;若只想保留一个,可将参数设为FALSE)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 05:15:27