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

相关推荐
沐知全栈开发3 分钟前
PHP Math: 精通PHP中的数学函数与应用
开发语言
吴声子夜歌14 分钟前
JavaScript——call()、apply()和bind()
开发语言·前端·javascript
平凡灵感码头17 分钟前
C语言 printf 数据打印格式速查表
c语言·开发语言·算法
兮℡檬,1 小时前
答题卡识别判卷
开发语言·python·计算机视觉
酉鬼女又兒1 小时前
零基础快速入门前端DOM 操作核心知识与实战解析(完整汇总版)(可用于备赛蓝桥杯Web应用开发)
开发语言·前端·javascript·职场和发展·蓝桥杯·js
kyle~1 小时前
C++----函数指针与函数指针类型 返回值类型 (*类型名)(参数列表)
开发语言·c++
努力中的编程者1 小时前
二叉树(C语言底层实现)
c语言·开发语言·数据结构·c++·算法
大尚来也2 小时前
PHP 反序列化漏洞深度解析:从原理利用到 allowed_classes 防御实战
android·开发语言·php
雕刻刀2 小时前
ERROR: Failed to build ‘natten‘ when getting requirements to build wheel
开发语言·python
qq_416018722 小时前
高性能密码学库
开发语言·c++·算法