Skip to content

Commit 47070c3

Browse files
committed
add an example
1 parent 0f66a86 commit 47070c3

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

docs/src/man/gallery.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,37 @@ julia> a1.result = reduce((x,y)-> x .|= byrow(a1, in, r"2", item = y, eq = eq),
153153
1
154154
```
155155

156+
* [Creating new column by selecting and matching](https://stackoverflow.com/questions/70948067/creating-new-column-in-r-dataframe-by-selecting-and-matching) : I want to use the option columns 1,2,3 to pull the appropriate values from length, weight, height.
157+
158+
```julia
159+
julia> using DLMReader, Chain
160+
julia> ds = filereader(IOBuffer("""item,length,width,height,color,option_1,option_2,option_3
161+
Box 1,2,4,6,,length,width,height
162+
Tape,10,3,,clear,width,length,color
163+
Pen,,,,red,color,,
164+
"""));
165+
166+
julia> @chain ds begin
167+
filter(2:4, by = !ismissing, type = any, view = true) # filter rows with dimensions
168+
modify!(Ref(2:4) .=> [byrow(select, with = 6), byrow(select, with = 7), byrow(select, with = 8)] .=> [:_t1, :_t2, :_t3], # extract dimensions
169+
r"_t" => byrow(join, delim = " × ") => :size, # create :size
170+
:size => byrow(x->"size : " * replace(x, r" × $"=>"")) # remove orphans " × " at the end
171+
)
172+
filter(parent(_), 5, by = !ismissing, view = true) # filter rows with colours
173+
modify!(:color => byrow(x->"color : " * x) => :col) # create :col
174+
modify!(parent(_), [:size, :col]=>byrow(x->ismissing(x[1]) ? x[2] : ismissing(x[2]) ? x[1] : x[1] * ", " * x[2])=>:option_set) # join :size and :col
175+
select!(Not([:size, :col]), Not(r"_t")) # remove temp columns
176+
end
177+
3×9 Dataset
178+
Row │ item length width height color option_1 option_2 option_3 option_set
179+
│ identity identity identity identity identity identity identity identity identity
180+
│ String? Int64? Int64? Int64? String? String? String? String? String?
181+
─────┼──────────────────────────────────────────────────────────────────────────────────────────────────────────────
182+
1 │ Box 1 2 4 6 missing length width height size : 2 × 4 × 6
183+
2 │ Tape 10 3 missing clear width length color size : 3 × 10, color : clear
184+
3 │ Pen missing missing missing red color missing missing color : red
185+
```
186+
156187
## Filtering
157188

158189
* [Filtering based on conditions comparing one column to other columns](https://discourse.julialang.org/t/dataframe-filtering-based-on-conditions-comparing-one-column-to-other-columns/70802) : In the following example we like to filter rows where columns `:x1` and `:x2` are greater than `:x5`.

0 commit comments

Comments
 (0)