Code
include("utils.jl")[ Info: loading success
include("utils.jl")[ Info: loading success
R"""
# Seed
set.seed(1)
# Data
customers <- sample(50:150, 10)
potential_customers <- sample(150:500, 10)
company <- LETTERS[1:10]
# Data frame
df <- data.frame(company = company,
x = customers,
y = potential_customers)
"""
df=@rget df
rename!(df,:x=>:customers,:y=>:potential_customers)
sort(df,:company)
#df2=@pivot_longer(df, customers:potential_customers)| Row | company | customers | potential_customers |
|---|---|---|---|
| String | Int64 | Int64 | |
| 1 | A | 117 | 234 |
| 2 | B | 88 | 426 |
| 3 | C | 50 | 479 |
| 4 | D | 83 | 412 |
| 5 | E | 136 | 478 |
| 6 | F | 92 | 228 |
| 7 | G | 63 | 362 |
| 8 | H | 131 | 186 |
| 9 | I | 108 | 254 |
| 10 | J | 100 | 366 |
fig=Figure()
ax=Axis(fig[1,1])
# for (idx,row) in enumerate(eachrow(df))
# hlines!(ax,[idx]; xmin =[row.customers], xmax = [row.potential_customers])
# end
# hlines!(ax, 1:10;xmin=[df.customers...], xmax =[df.potential_customers...], color = :blue)
fig@doc(@select)@select(df, exprs...)
Select variables in a DataFrame.
df: A DataFrame.exprs...: One or more unquoted variable names separated by commas. Variable names can also be used as their positions in the data, like x:y, to select a range of variables.julia> df = DataFrame(a = repeat('a':'e'), b = 1:5, c = 11:15);
julia> @chain df begin
@select(a, b, c)
end
5×3 DataFrame
Row │ a b c
│ Char Int64 Int64
─────┼────────────────────
1 │ a 1 11
2 │ b 2 12
3 │ c 3 13
4 │ d 4 14
5 │ e 5 15
julia> @chain df begin
@select(a:b)
end
5×2 DataFrame
Row │ a b
│ Char Int64
─────┼─────────────
1 │ a 1
2 │ b 2
3 │ c 3
4 │ d 4
5 │ e 5
julia> @chain df begin
@select(1:2)
end
5×2 DataFrame
Row │ a b
│ Char Int64
─────┼─────────────
1 │ a 1
2 │ b 2
3 │ c 3
4 │ d 4
5 │ e 5
julia> @chain df begin
@select(-(a:b))
end
5×1 DataFrame
Row │ c
│ Int64
─────┼───────
1 │ 11
2 │ 12
3 │ 13
4 │ 14
5 │ 15
julia> @chain df begin
@select(!(a:b))
end
5×1 DataFrame
Row │ c
│ Int64
─────┼───────
1 │ 11
2 │ 12
3 │ 13
4 │ 14
5 │ 15
julia> @chain df begin
@select(contains("b"), starts_with("c"))
end
5×2 DataFrame
Row │ b c
│ Int64 Int64
─────┼──────────────
1 │ 1 11
2 │ 2 12
3 │ 3 13
4 │ 4 14
5 │ 5 15
julia> @chain df begin
@select(-(1:2))
end
5×1 DataFrame
Row │ c
│ Int64
─────┼───────
1 │ 11
2 │ 12
3 │ 13
4 │ 14
5 │ 15
julia> @chain df begin
@select(!(1:2))
end
5×1 DataFrame
Row │ c
│ Int64
─────┼───────
1 │ 11
2 │ 12
3 │ 13
4 │ 14
5 │ 15
julia> @chain df begin
@select(-c)
end
5×2 DataFrame
Row │ a b
│ Char Int64
─────┼─────────────
1 │ a 1
2 │ b 2
3 │ c 3
4 │ d 4
5 │ e 5
julia> @chain df begin
@select(-contains("a"))
end
5×2 DataFrame
Row │ b c
│ Int64 Int64
─────┼──────────────
1 │ 1 11
2 │ 2 12
3 │ 3 13
4 │ 4 14
5 │ 5 15
julia> @chain df begin
@select(!contains("a"))
end
5×2 DataFrame
Row │ b c
│ Int64 Int64
─────┼──────────────
1 │ 1 11
2 │ 2 12
3 │ 3 13
4 │ 4 14
5 │ 5 15