假设我的 DataFrame 有两列v
和g
。首先,我按列对 DataFrame 进行分组g
并计算列的总和v
。其次,我使用了该功能maximum
来检索最大总和。我想知道是否可以一步检索该值?谢谢。
julia> using Random\n\njulia> Random.seed!(1)\nTaskLocalRNG()\n\njulia> dt = DataFrame(v = rand(15), g = rand(1:3, 15))\n15\xc3\x972 DataFrame\n Row \xe2\x94\x82 v g \n \xe2\x94\x82 Float64 Int64 \n\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xbc\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\n 1 \xe2\x94\x82 0.0491718 3\n 2 \xe2\x94\x82 0.119079 2\n 3 \xe2\x94\x82 0.393271 2\n 4 \xe2\x94\x82 0.0240943 3\n 5 \xe2\x94\x82 0.691857 2\n 6 \xe2\x94\x82 0.767518 2\n 7 \xe2\x94\x82 0.087253 1\n 8 \xe2\x94\x82 0.855718 1\n 9 \xe2\x94\x82 0.802561 3\n 10 \xe2\x94\x82 0.661425 1\n 11 …
Run Code Online (Sandbox Code Playgroud) 给出以下 ggplot 图,
p <- ggplot(mtcars, aes(mpg, wt)) + geom_point(aes(colour = factor(cyl)))
Run Code Online (Sandbox Code Playgroud)
如果我想更改图例的标签,我可以执行以下操作:
p + scale_color_manual(labels = c("X", "Y", "Z"), values = 1:3)
Run Code Online (Sandbox Code Playgroud)
但是,如果我只想更改标签而保持颜色不变,该怎么办?我尝试使用以下公式:
p + scale_color_manual(labels = c("X", "Y", "Z"))
Run Code Online (Sandbox Code Playgroud)
它给出了一个错误,说:Error in f(...) : argument "values" is missing, with no default
。
当然,我可以通过设置因子来实现这一点cyl
:
mtcars$cyl = as.factor(mtcars$cyl, labels = c("X", "Y", "Z"))
ggplot(mtcars, aes(mpg, wt)) + geom_point(aes(colour = factor(cyl)))
Run Code Online (Sandbox Code Playgroud)
但是,我想知道是否可以scale_color_manual
通过其他方式实现这一目标。
假设我有一个data.frame df
:
df <- data.frame(
A = 1:8,
B = rep(letters[1:4], each = 2),
C = rep(1:2, 4)
)
A B C
1 1 a 1
2 2 a 2
3 3 b 1
4 4 b 2
5 5 c 1
6 6 c 2
7 7 d 1
8 8 d 2
Run Code Online (Sandbox Code Playgroud)
我想交换列中的值B
,当它们是b
或时c
,而不是它们是什么时候,a
或者d
以另一列为条件C
,即何时C = 1
.所以目标data.frame是df1
:
df1 <- data.frame(
A = 1:8, …
Run Code Online (Sandbox Code Playgroud) 我现在正在观看 Lionel Zoubritzky 在 JuliaCon 2018 上发表的Engineering Julia for Speed演讲。演讲中提到了一个名为removetype
about 的函数13:24
。我想知道这个函数是如何定义的?谢谢。
(1)(2)
和之间有什么区别,x = 1; (x)(2)
如下所示?
julia> (1)(2)
2
# but
julia> x = 1
1
julia> (x)(2)
ERROR: MethodError: objects of type Int64 are not callable
Run Code Online (Sandbox Code Playgroud)
谢谢。
引自这里。