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

相关推荐
扶苏-su11 分钟前
Java--转换流-InputStreamReader 和 OutputStreamWriter
java·开发语言
无限进步_29 分钟前
【C语言&数据结构】二叉树遍历:从前序构建到中序输出
c语言·开发语言·数据结构·c++·算法·github·visual studio
花北城1 小时前
【C#】MES消耗类数量逻辑处理(物料消耗、打包装箱、生产订单派工等)
开发语言·c#
半夏知半秋1 小时前
kcp学习-skynet中的kcp绑定
开发语言·笔记·后端·学习
扶苏-su1 小时前
Java--标准输入输出流
java·开发语言
奋斗的小青年!!2 小时前
Flutter跨平台开发OpenHarmony应用:个人中心实现
开发语言·前端·flutter·harmonyos·鸿蒙
石头wang2 小时前
jmeter java.lang.OutOfMemoryError: Java heap space 修改内存大小,指定自己的JDK
java·开发语言·jmeter
LawrenceLan2 小时前
Flutter 零基础入门(十五):继承、多态与面向对象三大特性
开发语言·前端·flutter·dart
zh_xuan2 小时前
kotlin对象表达式
开发语言·kotlin
froginwe112 小时前
ECharts 旭日图:全面解析与应用指南
开发语言