R语言自定义vlookup函数

r 复制代码
##############################################################
#######自定义函数integrate_and_match_values用于提取数据
#############################################################
integrate_and_match_values <- function(target_data,target_id_col,target_count_column,source_data, source_id_col, source_count_column) {
  # Creating a dictionary from the source data
  result_dictionary <- setNames(source_data[[source_count_column]], source_data[[source_id_col]])
  
  # Matching and updating values in the target data
  target_data[[target_count_column]] <- result_dictionary[as.character(target_data[[target_id_col]])]
  
  # Replace any NA values that arise from unmatched IDs
  target_data[[target_count_column]][is.na(target_data[[target_count_column]])] <- NA  # This line can be omitted if NAs are okay
  
  return(target_data)
}

# Usage example:
# Assuming 'dfg4_1' has an 'Id' column and a 'Count' column, and 'dftotal' has an 'Id' column for matching
#使用dftotal去dfg4_1中提取数据框
#integrate_and_match_values函数里面的source是被提取的,target是鱼饵
dftotal <- integrate_and_match_values(dftotal,"Id", "Count_rg4",dfg4_1,"Id", "Count")
dftotal <- integrate_and_match_values(dftotal, "Id", "Count_uorf",dfuorf_1,"Id", "Count")
dftotal <- integrate_and_match_values(dftotal, "Id", "Score",dfg4_2,"Id", "Score")

对于代码上述代码,我先自定义一个函数,integrate_and_match_values(),其中dftotal是鱼饵,里面只有一列Id,我希望用dftotal里面的Id列去dfg4_1里面提取Id列对应的Count列,提取的Count列命名为Count_rg4,然后再去dfuorf_1中提取Count,命名为Count_uorf,然后再去dfg4_2中提取Score,命名为Score

相关推荐
FQNmxDG4S6 小时前
Java多线程编程:Thread与Runnable的并发控制
java·开发语言
前端老石人6 小时前
HTML 字符引用完全指南
开发语言·前端·html
matlab_xiaowang6 小时前
Redux 入门:JavaScript 可预测状态管理库
开发语言·javascript·其他·ecmascript
虹科网络安全6 小时前
艾体宝干货|数据复制详解:类型、原理与适用场景
java·开发语言·数据库
axng pmje7 小时前
Java语法进阶
java·开发语言·jvm
老前端的功夫7 小时前
【Java从入门到入土】28:Stream API:告别for循环的新时代
java·开发语言·python
qq_435287927 小时前
第9章 夸父逐日与后羿射日:死循环与进程终止?十个太阳同时值班的并行冲突
java·开发语言·git·死循环·进程终止·并行冲突·夸父逐日
止语Lab7 小时前
从手动到框架:Go DI 演进的三个拐点
开发语言·后端·golang
yaoxin5211238 小时前
397. Java 文件操作基础 - 创建常规文件与临时文件
java·开发语言·python
小短腿的代码世界8 小时前
Qt日志系统深度解析:从qDebug到企业级日志框架
开发语言·qt