模拟每列具有不同序列的数据框

Simulate a data frame with different sequence per column

我想在 R 中模拟一个具有 4 列的数据框,并满足上述条件:

  1. 每行总和为 1

  2. 第 1 列的第一个元素从接近 1 开始,比如 0.9,但每行逐渐减小。

  3. 其他3列的元素从低开始,在0.02-0.05之间,但随着每一行逐渐增加。

  4. 我希望数据框的最后一行是 c(0.25, 0.25, 0.25, 0.25)。

你能帮我创造这样的东西吗? 提前致谢!

# State the number of rows you want to create
rows <- 1000

# Create 4 randomly generated columns, the first ranging [.25, 1], the others [0, .25] and add .25 as the last row
x1<-c(runif(rows-1,.25,1), .25)
x2<-c(runif(rows-1,0,.25), .25)
x3<-c(runif(rows-1,0,.25), .25)
x4<-c(runif(rows-1,0,.25), .25)

library(dplyr)
# Combine the columns into a single table
df <- data.frame(x1, x2, x3, x4)

df %>% 
  # Sort each column individually (meaning the sorting of 1 column doesn't affect another)
  mutate(x1 = sort(x1, decreasing = T)) %>%
  mutate(across(x2:x4, ~sort(.x))) %>%
  # Transpose the table so you can sort the rows
  t() %>%
  as_tibble() %>%
  mutate_all(.funs = ~sort(., decreasing = T)) %>%
  t() %>%
  as_tibble() %>%
  # Scale the table you have so the sum of each row equals to 1
  mutate_all(.funs = ~ ./rowSums(across(everything())))