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

如何为R语言表格行自动生成6个随机数值(替代手动指定)

自动生成学生成绩表中的随机数

我希望无需逐个指定数值,就能为表格中的行生成6个随机数。目前我已手动为“Heather”和“Laurel”两行填写了固定数值,现寻求通过代码自动生成对应随机数的实现方式,当前使用的R语言代码如下:

student_names = c("Henry", "Cat", "Shelby", "Eren", "Eva", "Carter", "Dustin")
n_students <- length(student_names)

grades <- tibble(
  name = student_names,
  HW1 = rnorm(n_students, mean = 80, sd = 2) %>% round(0),
  HW2 = rnorm(n_students, mean = 80, sd = 5) %>% round(0),
  HW3 = rnorm(n_students, mean = 90, sd = 8) %>% round(0),
  HW4 = rnorm(n_students, mean = 60, sd = 10) %>% round(0),
  HW5 = rnorm(n_students, mean = 80, sd = 10) %>% round(0),
  HW6 = rnorm(n_students, mean = 90, sd = 5) %>% round(0)
)

grades <- rbind(grades, list("Heather", 100, 100, 100, 92, 95, 99))
grades <- rbind(grades, list("Laurel", 100, 100, 100, 100, 99, 105))

方法1:一次性生成所有学生的随机成绩

把Heather和Laurel加入学生名单,直接批量生成所有成绩,彻底避免手动输入固定值:

library(tibble)
library(dplyr)

# 包含所有学生的完整名单
student_names <- c("Henry", "Cat", "Shelby", "Eren", "Eva", "Carter", "Dustin", "Heather", "Laurel")
n_students <- length(student_names)

grades <- tibble(
  name = student_names,
  HW1 = rnorm(n_students, mean = 80, sd = 2) %>% round(0),
  HW2 = rnorm(n_students, mean = 80, sd = 5) %>% round(0),
  HW3 = rnorm(n_students, mean = 90, sd = 8) %>% round(0),
  HW4 = rnorm(n_students, mean = 60, sd = 10) %>% round(0),
  HW5 = rnorm(n_students, mean = 80, sd = 10) %>% round(0),
  HW6 = rnorm(n_students, mean = 90, sd = 5) %>% round(0)
)

方法2:分步添加指定学生的随机成绩

如果需要保留先生成部分学生、再补充Heather和Laurel的逻辑,推荐用add_row()(tidyverse标准方法,避免rbind()导致的类型转换问题),为两个学生单独生成随机成绩:

library(tibble)
library(dplyr)

student_names <- c("Henry", "Cat", "Shelby", "Eren", "Eva", "Carter", "Dustin")
n_students <- length(student_names)

grades <- tibble(
  name = student_names,
  HW1 = rnorm(n_students, mean = 80, sd = 2) %>% round(0),
  HW2 = rnorm(n_students, mean = 80, sd = 5) %>% round(0),
  HW3 = rnorm(n_students, mean = 90, sd = 8) %>% round(0),
  HW4 = rnorm(n_students, mean = 60, sd = 10) %>% round(0),
  HW5 = rnorm(n_students, mean = 80, sd = 10) %>% round(0),
  HW6 = rnorm(n_students, mean = 90, sd = 5) %>% round(0)
)

# 为Heather生成随机成绩并添加
grades <- grades %>%
  add_row(
    name = "Heather",
    HW1 = round(rnorm(1, 80, 2)),
    HW2 = round(rnorm(1, 80, 5)),
    HW3 = round(rnorm(1, 90, 8)),
    HW4 = round(rnorm(1, 60, 10)),
    HW5 = round(rnorm(1, 80, 10)),
    HW6 = round(rnorm(1, 90, 5))
  )

# 为Laurel生成随机成绩并添加
grades <- grades %>%
  add_row(
    name = "Laurel",
    HW1 = round(rnorm(1, 80, 2)),
    HW2 = round(rnorm(1, 80, 5)),
    HW3 = round(rnorm(1, 90, 8)),
    HW4 = round(rnorm(1, 60, 10)),
    HW5 = round(rnorm(1, 80, 10)),
    HW6 = round(rnorm(1, 90, 5))
  )

额外提示

  • 如果需要Heather和Laurel的成绩有不同的均值/标准差,直接修改对应rnorm()的参数即可;
  • 可以在代码开头添加set.seed(123)(数字可自定义),固定随机种子,保证每次运行生成的随机数完全一致。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 01:14:56