Julia 《朱莉娅》中的Excel vlookup

Julia 《朱莉娅》中的Excel vlookup,julia,vlookup,Julia,Vlookup,在Julia中有两个数组,X=Array{Float64,2}和Y=Array{Float64,2}。我想根据Excel功能执行vlookup。我似乎找不到类似的东西。下面的代码使用主矩阵中的相关记录从s的详细信息矩阵返回第一个匹配的值 function vlook(master, detail, val) val = master[findfirst(x->x==val,master[:,2]),1] return detail[findfirst(x->x==val,de

在Julia中有两个数组,X=Array{Float64,2}和Y=Array{Float64,2}。我想根据Excel功能执行vlookup。我似乎找不到类似的东西。

下面的代码使用主矩阵中的相关记录从s的详细信息矩阵返回第一个匹配的值

function vlook(master, detail, val)
  val = master[findfirst(x->x==val,master[:,2]),1]
  return detail[findfirst(x->x==val,detail[:,1]),2]
end


一种更通用的方法是使用,用于处理表格数据。

VLOOKUP是Excel用户常用的函数,具有以下特征:

VLOOKUP(lookup_value,table_array,col_index_num,range_lookup)
我从来都不太喜欢最后一个参数
range\u lookup
。首先,我不清楚“range_lookup”的意思是什么,其次,它是一个可选参数,默认值不太可能是近似匹配所需的值TRUE,而不是精确匹配所需的值FALSE

因此,在我尝试在Julia中编写VLOOKUP等价物时,我删除了
range\u lookup
参数,并添加了另一个参数
keycol\u index\u num
,以允许搜索
表数组
的第一列以外的其他列

警告 我是朱莉娅的新手,所以下面的代码中可能有一些嚎叫者。但它似乎对我有用。朱莉娅0.6.4。此外,正如前面所评论的,使用DataFrames可能是在类似数组的结构中查找值的更好解决方案

#=
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 Procedures: vlookup and vlookup_withfailsafe
Purpose : Inspired by Excel VLOOKUP. Searches a column of table_array for
  lookup_values and returns the corresponding elements from another column of
  table_array.

Arguments:
    lookup_values: a value or array of values to be searched for inside 
     column keycol_index_num of table_array.
    table_array: An array with two dimensions.
    failsafe: a single value. The return contains this value whenever an element
     of lookup_values is not found.
    col_index_num: the number of the column of table_array from which values
     are returned.
    keycol_index_num: the number of the column of table_array to be searched.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
=#
vlookup = function(lookup_values, table_array::AbstractArray, col_index_num::Int = 2, keycol_index_num::Int = 1)
    if ndims(table_array)!=2
         error("table_array must have 2 dimensions")
    end
    if isa(lookup_values,AbstractArray)
        indexes = indexin(lookup_values,table_array[:,keycol_index_num])
        if(any(indexes==0))
            error("at least one element of lookup_values not found in column $keycol_index_num of table_array")
        end
        return(table_array[indexes,col_index_num])
    else
        index = indexin([lookup_values],table_array[:,keycol_index_num])[1]
        if(index==0)
            error("lookup_values not found in column $keycol_index_num of table_array")
        end
        return(table_array[index,col_index_num])
    end
end

vlookup_withfailsafe = function(lookup_values, table_array::AbstractArray, failsafe, col_index_num::Int = 2, keycol_index_num::Int = 1)
    if ndims(table_array)!=2
        error("table_array must have 2 dimensions")
    end
    if !isa(failsafe,eltype(tablearray))
        error("failsafe must be of the same type as the elements of table_array")
    end
    if isa(lookup_values,AbstractArray)
        indexes = indexin(lookup_values,table_array[:,keycol_index_num])
        Result = Array{eltype(table_array)}(size(lookup_values))
        for i in 1:length(lookup_values)
            if(indexes[i]==0)
                Result[i] = failsafe
            else
                Result[i] = table_array[indexes[i],col_index_num]
            end
        end
        return(Result)
    else
        index = indexin([lookup_values],table_array[:,keycol_index_num])[1]
        if index == 0
            return(failsafe)
        else
            return(table_array[index,col_index_num])
        end
    end
end

您可以尝试组合使用Julia中的许多find函数:为什么不使用
函数定义它呢?@FelipeLema,我这么做了。谢谢。但这假设我正在尝试匹配单个值,而vlookup真正做的是匹配一列中的所有值(我不必指定一个值)假设存在匹配。Excel中的Vlookup更像是“使用矩阵a中的键3匹配矩阵b的第一列。返回矩阵b中出现此匹配的第n列中的值”。这在您的解决方案中并不是实际情况。我将研究使用数据帧,谢谢。
#=
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 Procedures: vlookup and vlookup_withfailsafe
Purpose : Inspired by Excel VLOOKUP. Searches a column of table_array for
  lookup_values and returns the corresponding elements from another column of
  table_array.

Arguments:
    lookup_values: a value or array of values to be searched for inside 
     column keycol_index_num of table_array.
    table_array: An array with two dimensions.
    failsafe: a single value. The return contains this value whenever an element
     of lookup_values is not found.
    col_index_num: the number of the column of table_array from which values
     are returned.
    keycol_index_num: the number of the column of table_array to be searched.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
=#
vlookup = function(lookup_values, table_array::AbstractArray, col_index_num::Int = 2, keycol_index_num::Int = 1)
    if ndims(table_array)!=2
         error("table_array must have 2 dimensions")
    end
    if isa(lookup_values,AbstractArray)
        indexes = indexin(lookup_values,table_array[:,keycol_index_num])
        if(any(indexes==0))
            error("at least one element of lookup_values not found in column $keycol_index_num of table_array")
        end
        return(table_array[indexes,col_index_num])
    else
        index = indexin([lookup_values],table_array[:,keycol_index_num])[1]
        if(index==0)
            error("lookup_values not found in column $keycol_index_num of table_array")
        end
        return(table_array[index,col_index_num])
    end
end

vlookup_withfailsafe = function(lookup_values, table_array::AbstractArray, failsafe, col_index_num::Int = 2, keycol_index_num::Int = 1)
    if ndims(table_array)!=2
        error("table_array must have 2 dimensions")
    end
    if !isa(failsafe,eltype(tablearray))
        error("failsafe must be of the same type as the elements of table_array")
    end
    if isa(lookup_values,AbstractArray)
        indexes = indexin(lookup_values,table_array[:,keycol_index_num])
        Result = Array{eltype(table_array)}(size(lookup_values))
        for i in 1:length(lookup_values)
            if(indexes[i]==0)
                Result[i] = failsafe
            else
                Result[i] = table_array[indexes[i],col_index_num]
            end
        end
        return(Result)
    else
        index = indexin([lookup_values],table_array[:,keycol_index_num])[1]
        if index == 0
            return(failsafe)
        else
            return(table_array[index,col_index_num])
        end
    end
end