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

相关推荐
Rain50910 分钟前
mini-cc 的 MCP 协议:给 AI 装个 USB-C 接口
c语言·开发语言·前端·人工智能·架构·node.js·ai编程
华科大胡子30 分钟前
AI开发者的网络卡点:Anthropic连接超时
开发语言·php
磊 子1 小时前
STL无序关联容器—unorded_set+unorded_map
开发语言·c++
AI人工智能+电脑小能手1 小时前
【大白话说Java面试题 第84题】【Mysql篇】第14题:为什么用 InnoDB 存储引擎的表建议用整型的自增主键?
java·开发语言·数据库·mysql·面试
YikNjy2 小时前
break和continue
java·开发语言·算法
秋92 小时前
java项目中cpu飙升排查及解决方法
java·开发语言
野生技术架构师2 小时前
牛客网2026最新大厂Java高频面试题精选(附标准答案)
java·开发语言
PH = 72 小时前
JAVA的SPI机制
java·开发语言
IT猿手2 小时前
多目标优化算法:多目标蛇优化算法(Multiple Objective Snake Optimizer,MOSO)(提供MATLAB代码)
开发语言·算法·matlab·动态路径规划·光伏模型参数估计
朔北之忘 Clancy2 小时前
2026 年 3 月青少年软编等考 C/C++ 一级真题解析
c语言·开发语言·c++·青少年编程·题解·考级