当全局环境中的变量恰好mutate与$提取的元素同名时,我使用提取得到了有趣的结果.(我正在运行R 3.1.3和dplyr 0.4.3.9.)这很好用:
library(dplyr)
df <- data.frame(time = 1:5, val = c(2.3, 3.9, NA, 8.1, 9.6))
mutate(df, val = approx(time, val, time)$y)
# time val
# 1 1 2.3
# 2 2 3.9
# 3 3 6.0
# 4 4 8.1
# 5 5 9.6
Run Code Online (Sandbox Code Playgroud)
但是,如果我定义一个全局变量y,有趣的事情会发生:
y <- 1L
mutate(df, val = approx(time, val, time)$y)
# Error: invalid subscript type 'integer'
Run Code Online (Sandbox Code Playgroud)
请注意,使用带有字符串参数的双括号仍然按预期工作:
mutate(df, val = approx(time, val, time)[['y']])
# time val
# 1 1 …Run Code Online (Sandbox Code Playgroud) 我最近开始使用VS Code,我注意到滚动条中有很少的彩色像素,如下所示:
它们似乎表明了源代码的一些内容,但我无法找到相关的文档.所以我的问题如下:
编辑:
scm.diffDecorations到"none"并重新启动VS代码,重新打开文件等,但装饰仍然存在."editor.hideCursorInOverviewRuler"到true,但事实证明,控制不同的功能.此外,我已经"editor.minimap.enabled"设置false,但小地图是与滚动条装饰不同的功能.我在 Julia 中编写类时遇到问题。我看过文档,但没有看到任何关于课程的文档。
在 Python 中,类是,例如,
class Dog:
# ----blah blah---
Run Code Online (Sandbox Code Playgroud)
这在 Julia 中怎么可能?
考虑findfirst功能。我可以findfirst使用methods以下方法查看定义了哪些方法:
julia> methods(findfirst)
# 9 methods for generic function "findfirst":
[1] findfirst(A::Union{AbstractString, AbstractArray}) in Base at array.jl:1672
[2] findfirst(p::Union{Base.Fix2{typeof(==),T}, Base.Fix2{typeof(isequal),T}}, r::StepRange{T,S}) where {T, S} in Base at array.jl:1758
[3] findfirst(pred::Base.Fix2{#s66,#s65} where #s65<:Union{Int8, UInt8} where #s66<:Union{typeof(==), typeof(isequal)}, a::Union{Array{Int8,1}, Array{UInt8,1}}) in Base at strings/search.jl:22
[4] findfirst(testf::Function, A::Union{AbstractString, AbstractArray}) in Base at array.jl:1754
[5] findfirst(testf::Function, A) in Base at array.jl:1747
[6] findfirst(pattern::AbstractString, string::AbstractString) in Base at strings/search.jl:104
[7] findfirst(ch::AbstractChar, string::AbstractString) in Base at strings/search.jl:124
[8] findfirst(r::Regex, s::AbstractString) …Run Code Online (Sandbox Code Playgroud) 根据Julia文档的相关部分,我的理解是,像这样的非标准字符串文字foo"hello, world"等效于显式调用相应的宏:@foo_str("hello, world")。但是,肯定还有一些我不了解的魔术。考虑日期格式dateformat"\m"。本身"\m"会引发语法错误:
julia> "\m"
ERROR: syntax: invalid escape sequence
Run Code Online (Sandbox Code Playgroud)
如果我调用@dateformat_str("\m"),则会引发相同的语法错误,因为字符串文字"\m"在传递给宏之前似乎已经过评估或检查了错误:
julia> using Dates
julia> @dateformat_str("\m")
ERROR: syntax: invalid escape sequence
Run Code Online (Sandbox Code Playgroud)
但是,使用非标准字符串文字可以工作:
julia> dateformat"\m"
dateformat"\m"
Run Code Online (Sandbox Code Playgroud)
这是违反直觉的,因为我认为这dateformat"\m"等效于@dateformat_str("\m")。非标准字符串文字如何避免由标准字符串文字产生的语法错误?
该RecipesBase.jl @recipe宏利用的ASCII字符,即构造出一组特殊的经营者-->和:=。这些字符序列似乎具有一些特殊的属性,可以将它们解析为Expr。比较-->到--:
julia> 1 --> 2
ERROR: syntax: invalid syntax 1 --> 2
julia> 1 -- 2
ERROR: syntax: invalid operator "--"
julia> :(1 --> 2)
:($(Expr(:-->, 1, 2)))
julia> :(1 -- 2)
ERROR: syntax: invalid operator "--"
Run Code Online (Sandbox Code Playgroud)
有趣的是,1 --> 2使用的表达式头解析:-->,而其他二进制运算符(包括Unicode二进制运算符,如?(类型为\uparrow+ TAB),则使用的表达式头解析:call:
julia> dump(:(1 --> 2))
Expr
head: Symbol -->
args: Array{Any}((2,))
1: Int64 1
2: Int64 …Run Code Online (Sandbox Code Playgroud) 我想在Julia中找到一种简洁的语法,以向量化的方式索引字典。在R中,我将执行以下操作:
dict <- c("a" = 1, "b" = 2)
keys <- c("a", "a", "b", "b", "a")
dict[keys]
Run Code Online (Sandbox Code Playgroud)
在茱莉亚,如果我有一个dict和keys这样的
dict = Dict(:a => 1, :b => 2)
keys = [:a, :a, :b, :b, :a]
Run Code Online (Sandbox Code Playgroud)
那么我可以使用列表理解来达到预期的结果:
julia> [dict[key] for key in keys]
5-element Array{Int64,1}:
1
1
2
2
1
Run Code Online (Sandbox Code Playgroud)
是否有更简洁的矢量化语法,类似于R语法?
Julia JIT 编译器为在 REPL 中调用的每个唯一函数签名编译单独版本的泛型函数。例如,如果foo定义为
foo(x, y) = (x * y) ^ 2
Run Code Online (Sandbox Code Playgroud)
然后调用foo(2, 3)和foo("a", "b")将编译分别foo对应于签名foo(::Int, ::Int)和 的两个不同版本foo(::String, ::String)。有没有办法获得为通用函数编译的不同函数签名的列表?
我知道这个methods函数,但methods只列出了程序员编写的通用函数的方法。而且,当然,如foo上面的示例所示,程序员编写的一个方法可以产生许多单独的编译函数。
我只是出于教育目的而问这个。我没有任何代码计划在其中使用显示已编译的所有函数签名的假设函数的结果。
有没有一种简单的方法可以从公式中删除响应变量?我试过使用stats::update.formula,像这样:
update(y ~ x, ~ .)
# y ~ x
Run Code Online (Sandbox Code Playgroud)
但是你可以看到上面没有从公式中删除响应变量。
CategoricalArrays.jl 中的CategoricalArray构造函数和categorical函数在行为上似乎几乎相同:
julia> using CategoricalArrays
julia> x = CategoricalArray(["a", "b", "c"]; ordered=true, levels=["c", "b", "a"])
3-element CategoricalArray{String,1,UInt32}:
"a"
"b"
"c"
julia> y = categorical(["a", "b", "c"]; ordered=true, levels=["c", "b", "a"])
3-element CategoricalArray{String,1,UInt32}:
"a"
"b"
"c"
julia> x == y
true
Run Code Online (Sandbox Code Playgroud)
有没有之间的显着差异CategoricalArray和categorical?如果它们基本相同,那么包含冗余categorical功能的原因是什么?