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

相关推荐
夜晚回家21 分钟前
Python 零基础入门 | 输入的数字为什么不能直接算
开发语言·python
时间的拾荒人23 分钟前
Qt 事件机制全解析:从事件处理到事件过滤器
开发语言·qt·面试
于指尖飞舞25 分钟前
java面试题总结
java·开发语言
zhangzeyuaaa2 小时前
深入理解 Ruby 运算符:本质、分类、坑点与重载实战
开发语言·前端·ruby
一木 之林2 小时前
DeepSeek Agent 开发(一)
开发语言·前端·javascript
朔北之忘 Clancy2 小时前
青少年软编等考七级题解目录
开发语言·c++·青少年编程·gesp·csp·信奥赛·noi
ss2732 小时前
AI全栈实战 | 3.2-01 Python 基础:四大数据容器怎么选,推导式为什么是 Pythonic 的灵魂
开发语言·人工智能·python
Mr_Macallon2 小时前
C++回顾(基础)(01)
c语言·开发语言·c++·学习
未来之窗软件服务2 小时前
Go 利器 GoReSym:剥离符号恢复 Go 二进制元数据—东方仙盟
开发语言·golang·仙盟创梦ide·东方仙盟
朝朝辞暮i3 小时前
C++ 第 32 课:main()、rclcpp::init()、spin()、shutdown()
开发语言·c++·算法