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

在R中合并模拟数据框得到<0 rows>结果的问题求助

解决R中合并模拟数据集得到0行结果的问题

问题原因

你使用merge()函数合并两个数据集,但merge()默认执行内连接(inner join)——仅保留两个数据框中所有列值完全匹配的行。由于method_0和method_1是独立模拟生成的,几乎不存在所有列值完全相同的行,因此合并结果为空。

你的需求是将两个方法的模拟数据纵向拼接(上下堆叠),而非按列匹配合并,因此应该使用行绑定函数。

修复后的代码

将simulate_data函数中的merged = merge(method_0, method_1)替换为rbind(method_0, method_1)即可解决问题。以下是完整修复后的代码:

library(MASS)
simulate_number <- function(p, theta, lambda, n){
  '
  Simulate_number
  
  Function that simulates data from a binomial distribution where 
  the number of trials are drawn from a negative-binomial distribution.
  
  ### Parameters ###
  p = the probability of success in the binomial trial
  
  theta = the theta variable in the negative-binomial trial
  
  lambda = is the average in the negative-binomial distribution this number is then multiplied by 20 to more accurately represent the real data.
  
  n = number of simulation runs
  
  ### Returns ###
  A data frame with entries of all the following parameters for each of the n simulations,
  
  '
  lambda <- lambda * 20
  p_hat <- rep(0, n)
  p_hat_t <- rep(0, n)
  Y_20 <- rep(0, n)
  m_true <- rep(0, n)
  m_actual <- rep(0, n)
  for(i in 1:n){
    m_true[i] <- rnegbin(1, lambda, theta)
    Y_20[i] <- rbinom(1, m_true[i], p)
    m_actual[i] <- rnegbin(1, lambda, theta)
    p_hat[i] <- Y_20/m_actual[i]
    p_hat_t[i] <- Y_20/m_true[i]
  }
  # 直接创建数据框,无需逐个转换后cbind
  result <- data.frame(p_hat, p_hat_t, m_true, m_actual, Y_20)
  return(result)
}

simulate_data <- function(p_0, p_1, theta, lambda, n){
  '
  Function that uses the function, simulate_number(p, theta, lambda, n) to create a data set 
  with two theoretical methods and saves them in a data frame with coding 0 and 1 for the two methods respectively.
  
  ### Parameters ###
  
  p_0 = the probability of a failure to remove a lice for the first method
  
  p_1 = the probability of a failure to remove a lice for the second method
  
  theta = the dispersion parameter for the neg-bin distribution
  
  lambda = the average number of lice on a fish
  
  n = the number of simulations
  
  ### Returns ###
  A data frame with the results from simulate_number for each method and a code of 0 or 1 denoting what method
  '
  
  method_0 <- simulate_number(p_0, theta, lambda, n)
  method_1 <- simulate_number(p_1, theta, lambda, n)
  
  # 添加分组编码并统一列名
  method_0$code <- as.factor(rep(0, n))
  method_1$code <- as.factor(rep(1, n))
  
  # 纵向拼接两个数据集
  merged <- rbind(method_0, method_1)
  
  return(merged)
}

# 测试函数
simulate_data(p_0 = .1, p_1 = .3, theta = 10, lambda = 2, n = 250)

额外优化建议

  • 变量赋值统一使用<-(R语言标准赋值符号),避免混用=和<-。
  • 简化simulate_number中的数据框创建逻辑,直接用data.frame()一次性组合所有变量,无需逐个转换为单列数据框再cbind。
  • 修正了注释中的拼写错误(如succsess→success、neg-binoimial→negative-binomial等),提升代码可读性。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 09:18:11