如何在IndexedTable中添加或编辑值?从the documentation中,我了解到IndexedTable对象本身是不可变的,但不是底层数据,因此我“理解”了为什么这样的方法不起作用,但是我不知道如何使用新数据获取新的IndexedTable :
myTable = ndsparse((
region = ["US","US","US","US","EU","EU","EU","EU"],
product = ["apple","apple","banana","banana","apple","apple","banana","banana"],
year = [2011,2010,2011,2010,2011,2010,2011,2010]
),(
production = [3.3,3.2,2.3,2.1,2.7,2.8,1.5,1.3],
consumption = [4.3,7.4,2.5,9.8,3.2,4.3,6.5,3.0]
))
myTable["EU","banana",2011] = (2.5, 7.5) # ERROR: type Tuple has no field region
myTable["EU","banana",2012] = (2.5, 7.5) # ERROR: type Tuple has no field region
myTable["EU","banana",2011] = (production = 2.5, consumption = 7.5) # ERROR: type Tuple has no field region
答案 0 :(得分:1)
您想要的功能似乎在JuliaDB的Base.merge
下。
merge(a::NDSparse, a::NDSparse; agg)
将a的行与b的行合并。要保留唯一键,b中的值 优先。提供的函数agg将汇总来自 和b具有相同的密钥。
Example: a = table((x = 1:5, y = rand(5)); pkey = :x) b = table((x = 6:10, y = rand(5)); pkey = :x) merge(a, b) a = ndsparse([1,3,5], [1,2,3]) b = ndsparse([2,3,4], [4,5,6]) merge(a, b) merge(a, b; agg = (x,y) -> x)
基于您的问题的工作示例:
# tested on Julia 1.0.4
julia> using JuliaDB
julia> myTable = ndsparse((
region = ["US","US","US","US","EU","EU","EU","EU"],
product = ["apple","apple","banana","banana","apple","apple","banana","banana"],
year = [2011,2010,2011,2010,2011,2010,2011,2010]
),(
production = [3.3,3.2,2.3,2.1,2.7,2.8,1.5,1.3],
consumption = [4.3,7.4,2.5,9.8,3.2,4.3,6.5,3.0]
))
3-d NDSparse with 8 values (2 field named tuples):
region product year │ production consumption
───────────────────────┼────────────────────────
"EU" "apple" 2010 │ 2.8 4.3
"EU" "apple" 2011 │ 2.7 3.2
"EU" "banana" 2010 │ 1.3 3.0
"EU" "banana" 2011 │ 1.5 6.5 # Note the old value
"US" "apple" 2010 │ 3.2 7.4
"US" "apple" 2011 │ 3.3 4.3
"US" "banana" 2010 │ 2.1 9.8
"US" "banana" 2011 │ 2.3 2.5
julia> updated_myTable = ndsparse((
region = ["EU"],
product = ["banana"],
year = [2011]
),(
production = [2.5], # new values here
consumption = [7.5]
))
3-d NDSparse with 1 values (2 field named tuples):
region product year │ production consumption
───────────────────────┼────────────────────────
"EU" "banana" 2011 │ 2.5 7.5
julia> newTable = merge(updated_myTable, myTable, agg = (x,y) -> x)
3-d NDSparse with 8 values (2 field named tuples):
region product year │ production consumption
───────────────────────┼────────────────────────
"EU" "apple" 2010 │ 2.8 4.3
"EU" "apple" 2011 │ 2.7 3.2
"EU" "banana" 2010 │ 1.3 3.0
"EU" "banana" 2011 │ 2.5 7.5 # Note the updated values here!
"US" "apple" 2010 │ 3.2 7.4
"US" "apple" 2011 │ 3.3 4.3
"US" "banana" 2010 │ 2.1 9.8
"US" "banana" 2011 │ 2.3 2.5
请注意,agg
函数在遇到冲突时如何偏爱第一个参数中的键。
另一种骇人听闻的方法是在找到正确的索引后直接编辑数据元素。
julia> i = findfirst(isequal((region = "EU", product = "banana", year = 2011)), myTable.index)
4
julia> myTable.data[i]
(production = 1.5, consumption = 6.5)
julia> myTable.data[i] = (production = 2.5, consumption = 7.5)
(production = 2.5, consumption = 7.5)
julia> myTable
3-d NDSparse with 8 values (2 field named tuples):
region product year │ production consumption
───────────────────────┼────────────────────────
"EU" "apple" 2010 │ 2.8 4.3
"EU" "apple" 2011 │ 2.7 3.2
"EU" "banana" 2010 │ 1.3 3.0
"EU" "banana" 2011 │ 2.5 7.5 # Note the updated values here!
"US" "apple" 2010 │ 3.2 7.4
"US" "apple" 2011 │ 3.3 4.3
"US" "banana" 2010 │ 2.1 9.8
"US" "banana" 2011 │ 2.3 2.5
希望有帮助。