我有这个简单的代码(数据+条形图):
dat <- c('Jan','Feb','Mar', 'Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec')
val <- c(-2.5, 3, 2.5, -3.3, -1, 0.2, 6, 4.3, 5.5, 2, -1.9, -2.3)
df <- data.frame(dat, val)
bar <- ggplot(data = df, aes(x = factor(dat, levels = month.abb), y = val)) +
geom_bar(stat = 'identity')
print(bar)
Run Code Online (Sandbox Code Playgroud)
在其他地方,我使用以下调色板创建了一个热图:
# Palette
LtoM <-colorRampPalette(c('red', 'yellow' ))
Mid <- "snow3"
MtoH <-colorRampPalette(c('lightgreen', 'darkgreen'))
Run Code Online (Sandbox Code Playgroud)
被称为:
scale_fill_gradient2(low = LtoM(100), mid = Mid, high = MtoH(100))
Run Code Online (Sandbox Code Playgroud)
现在我想为我的条形图使用类似的调色板,也就是说我希望每个条形图中的颜色是高度的函数(从最小y到最小y的红色的某些等级到绿色).
我该怎么办?
我正在运行 64 位 Windows 7 平台。
\n\n我已添加到我安装的程序/库中:
\n\n实时工具
R(软件)
Rcpp(R包)
内联(R 包)
我尝试了这里找到的一个片段
\n\n## now with Rcpp and C++\nlibrary(inline)\n# and de\xef\xac\x81ne our version in C++\nsrc <- "int n = as<int>(ns);\ndouble x = as<double>(xs);\nfor (int i=0; i<n; i++) x=1/(1+x);\nreturn wrap(x); "\nl <- cxxfunction(signature(ns="integer", xs="numeric"),\nbody=src, plugin="Rcpp")\nRun Code Online (Sandbox Code Playgroud)\n\n但这不起作用(说实话,这并不奇怪,因为我什至没有指定例如 Rtools 的位置)。我收到以下错误消息:
\n\nError in system(cmd, intern = !verbose) : \'C:/Program\' not found\nRun Code Online (Sandbox Code Playgroud)\n\n我不确定这意味着什么。而且,我已经被困在那里几个小时了。有人可以帮我一点吗?
\n我有两个与C++中标准库的vector类相关的问题.
如何检查向量中是否已存在某个值(例如一个整数)?
我想要的单词如下:"如果向量中已存在整数,则下一个,否则将其添加到向量的末尾."
如何应用包含向量中每个元素的参数的函数?(看来我不能用for_each做到这一点)
在单词中:"对于向量中的每个z元素,应用MyAddFn(i,j)"
...或者我可能没有使用stl向量序列容器在正确的轨道上,我应该定义自己的迭代器?
我有以下3个文件(1*.cpp和2*.hpp):
主程序文件:
// test.cpp
#include<iostream>
#include"first_func.hpp"
#include"sec_func.hpp"
int main()
{
double x;
x = 2.3;
std::cout << sec_func(x) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
- first_func.hpp标头:
// first_func.hpp
...
double first_func(double x, y, x)
{
return x + y + x;
}
Run Code Online (Sandbox Code Playgroud)
- sec_func.hpp标头:
// sec_func.hpp
...
double sec_func(double x)
{
double a, b, c;
a = 3.4;
b = 3.3;
c = 2.5;
return first_func(a,b,c) + x;
}
Run Code Online (Sandbox Code Playgroud)
如何在sec_func.hpp文件中正确调用first_func?
有一种优雅的方式(也许在numpy),以获得一个Python整数的给定部分,例如说,我想90从1990.
我可以:
my_integer = 1990
int(str(my_integer)[2:4])
# 90
Run Code Online (Sandbox Code Playgroud)
但它很难看.
还有其他选择吗?
我已经计算了价格系列的月度回报.然后我构建一个数据帧如下:
y.ret_1981 y.ret_1982 y.ret_1983 y.ret_1984 y.ret_1985
1 0.0001015229 0.0030780203 -0.0052233836 0.017128325 -0.002427308
2 0.0005678989 0.0009249838 -0.0023294622 -0.030531971 0.001831160
3 -0.0019040392 -0.0021614791 0.0022451252 -0.003345983 0.005773503
4 -0.0006015118 0.0010695681 0.0052680258 0.008592513 0.009867972
5 0.0052736054 -0.0003181347 -0.0008505673 -0.000623061 -0.012225140
6 0.0014266119 -0.0101045071 -0.0003073150 -0.016084505 -0.005883687
7 -0.0069002733 -0.0078170620 0.0070058676 -0.007870294 -0.010265335
8 -0.0041963258 0.0039905142 0.0134996961 -0.002149331 -0.007860940
9 0.0020778541 -0.0038834826 0.0052289589 0.007271409 -0.005320848
10 0.0030956487 -0.0005027686 -0.0021452210 0.002502301 -0.001890657
11 -0.0032375542 0.0063916686 0.0009331531 0.004679741 0.004338580
12 0.0014882164 0.0039578527 0.0136663415 0.000000000 0.003807668
Run Code Online (Sandbox Code Playgroud)
......其中列是1981年至1985年的月回报,第1至12行是一年中的月份.
我想绘制一个类似下面的框图: …
假设我使用函数geom_raster()(from ggplot2)创建了热图.
在表格底部添加一行的智能方法是什么(在我的情况下),在所考虑的时间内每个月的"平均回报"?
如果在1985年至2013年期间和平均行之间留下一些空间,那将是很好的,也许警察颜色和"案例"可以定制.
我的代码的核心如下(该对象molten包含我的数据,最初是通过melt()函数传递的矩阵reshape2.
hm <- ggplot(data = molten, aes(x = factor(Var2, levels = month.abb), y=Var1, fillll=value)) + geom_raster()
hm <- hm + scale_fill_gradient2(low=LtoM(100), mid=Mid, high=MtoH(100))
hm <- hm + labs(fill='% Return')
hm <- hm + geom_text(aes(label=paste(sprintf("%.1f %%", value))), size = 4)
hm <- hm + scale_y_continuous(breaks = 1985:2013)
hm <- hm + xlab(label = NULL) + ylab(label = NULL)
hm <- hm + theme_bw()
hm <- hm + theme(axis.text.x …Run Code Online (Sandbox Code Playgroud) 我有一个每日时间序列作为R 中的myxtsxts 对象,日期格式为 d/m/y。现在,我想将原始时间序列减少到只取序列中每个月的第一个日期和值的时间序列。
myxts[.indexmday(myxts) == 1] 返回包含 d/m/y 且 d = 1 的系列。
我的问题是我每个月需要 1 个数据,我的问题是我的原始系列有一些月份没有日期和第一个日历日期的数据。
如果没有这样的日期,我如何对R说,然后取这个特定月份的第二天,如果后者不可用,则取第三天,依此类推......
例如,在下面的示例数据中,没有 2014 年 2 月 1 日。也没有 1 月 1 日。
dates <- c('14/02/2014', '13/02/2014', '12/02/2014', '11/02/2014', '10/02/2014', '07/02/2014', '06/02/2014', '05/02/2014', '04/02/2014', '03/02/2014', '31/01/2014', '30/01/2014', '29/01/2014', '28/01/2014', '27/01/2014', '24/01/2014', '23/01/2014', '22/01/2014', '21/01/2014', '20/01/2014', '17/01/2014', '16/01/2014', '15/01/2014', '14/01/2014', '13/01/2014', '10/01/2014', '09/01/2014', '08/01/2014', '07/01/2014', '06/01/2014', '03/01/2014', '02/01/2014', '31/12/2013', '30/12/2013', '27/12/2013', '26/12/2013', '24/12/2013', '23/12/2013', '20/12/2013', '19/12/2013', '18/12/2013', '17/12/2013', '16/12/2013', …Run Code Online (Sandbox Code Playgroud) 我对以下内容感到困惑:
这有效:
a, b = 1071, 1029
while(a%b != 0):
a, b = b, a%b
Run Code Online (Sandbox Code Playgroud)
但是,以下代码段返回ZeroDivisionError错误消息:
a, b = 1071, 1029
while(a%b != 0):
a = b; b = a%b
Run Code Online (Sandbox Code Playgroud)
虽然我预计两者都是完全相同的.
有人可以对此嗤之以鼻吗?