我有两个列表 m.list 和另一个 r.list。m.list 有NA值。对于那些有NA值的人,我想用 r.list 中的元素替换它。问题是当我replace在 R 中使用函数时,它采用 r.list 的索引并返回不正确的值。以下是可重现的示例。有没有办法根据另一个列表中的元素名称替换一个列表的值?
m.list <- list(a= 1,b=NA,c=3,d=NA)
r.list <- list(a= 4,d=8,c=9)
mr.list <- replace(m.list, which(is.na(m.list)), r.list[which(is.na(m.list))])
Run Code Online (Sandbox Code Playgroud)
这是我得到的输出 b 应该是 NA 并且 d 应该是 8:
> mr.list
$a
[1] 1
$b
[1] 8
$c
[1] 3
$d
NULL
Run Code Online (Sandbox Code Playgroud)
这是所需的输出:
$a
[1] 1
$b
[1] NA
$c
[1] 3
$d
[1] 8
Run Code Online (Sandbox Code Playgroud) 我正在使用gnuplot绘制多个函数的轮廓图。这是为了优化问题。我有3个功能:
f(x,y) g1(x,y)g2(x,y)两者g1(x,y)和g2(x,y)都是约束条件,想在的等高线图上绘制f(x,y)。
这是教科书示例:
感谢@theozh,这是我在gnuplot中复制它的尝试。
### contour lines with labels
reset session
f(x,y)=(x**2+y-11)**2+(x+y**2-7)**2
g1(x,y)=(x-5)**2+y**2
g2(x,y) = 4*x+y
set xrange [0:6]
set yrange [0:6]
set isosample 250, 250
set key outside
set contour base
set cntrparam levels disc 10,30,75,150,300,500,850,1500
unset surface
set table $Contourf
splot f(x,y)
unset table
set contour base
set cntrparam levels disc 26
unset surface
set table $Contourg1
splot g1(x,y)
unset table
set contour base
set cntrparam levels disc 20 …Run Code Online (Sandbox Code Playgroud) 我的问题与这个问题有关.但是,上面引用的问题使用的multicore是被替换的包parallel.无法在parallel包中复制响应中的大多数功能.有没有办法跟踪进度mclapply.在查看mclapply文档时,有一个名为的参数mc.silent,我不确定这是否能够跟踪进度,如果可以,我们如何以及在哪里可以看到日志文件?我在ubuntulinux OS 上运行.请参阅下面的重复性示例,我想对其进行处理.
require(parallel)
wait.then.square <- function(xx){
# Wait for one second
Sys.sleep(2);
# Square the argument
xx^2 }
output <- mclapply( 1:10, wait.then.square, mc.cores=4,mc.silent=FALSE)
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激.
我有一个函数,我想调用它computeMASE 来应用于 3 个不同的列表,forecast.list,train.list,test.list所有列表都有共同的值 ( ap, wi)。我可以单独使用该函数到列表中,如下面的代码所示,但是当我使用lapply并应用该函数来一次获取所有数据时,我无法做到这一点。请参阅下面的可重现示例。请让我知道如何解决这个问题。
非常感谢
library("forecast")
## Forecast Function
for.x <- function(x){
fc <- forecast(ets(x),h=18)$mean
return(fc)
}
## MASE Function
computeMASE <- function(forecast,train,test,period){
# forecast - forecasted values
# train - data used for forecasting .. used to find scaling factor
# test - actual data used for finding MASE.. same length as forecast
# period - in case of seasonal data.. if not, use 1
forecast <- as.vector(forecast)
train …Run Code Online (Sandbox Code Playgroud) mathematica我们有RealDigits可以识别带小数点和整数值的第一个非零数字。请参阅下面的示例:
RealDigits[ 0.00318, 10, 1]
{{3},-2}
RealDigits[ 419, 10, 1]
{{4},-2}
Run Code Online (Sandbox Code Playgroud)
在上面的示例中,函数识别 3 和 4 分别表示 0.00318 和 419。
中是否有类似的功能R?
我有以下3x3方阵a和矢量x:
a = matrix(c(1.0, 0.1, 0.5,
0.1, 1.0, 0.9,
0.5, 0.9, 1.0), nrow=3, ncol=3)
x = c(0.1,0.2,0.3)
Run Code Online (Sandbox Code Playgroud)
我想基本上使用矩阵和向量的上三角形来遵循公式
y = (a12 - (x1/x2)^2 + (a13 - x1/x3)^2 + (a23 - x2/x3)^2
Run Code Online (Sandbox Code Playgroud)
我在上面的公式中使用了以下用于R的循环,我收到以下错误:
Error in a[i, j] : subscript out of bounds
Run Code Online (Sandbox Code Playgroud)
如果你能做错什么事我能不能告诉我,我将不胜感激.
## initialize y
y = 0
for (i in 1 : nrow(a)-1){
for (j in i+1 : nrow(a)){
y = y + ((a[i,j]) - (x[i]/x[j])^2
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 gnuplot 制作 2D 矢量动画。我想显示一行,即一次显示一个向量。
我的数据结构如下:它们x,y,u,v
2.24448 0.270645 1.00 1.00
3.24448 0.270645 0.500 1.20
Run Code Online (Sandbox Code Playgroud)
我可以使用以下命令创建静态图:
plot "datam.dat" using 1:2:3:4 with vectors filled head lw 3
Run Code Online (Sandbox Code Playgroud)
这是我的问题:我想制作动画并一次显示一行(即)一个向量,如何使用 GIF 在 GNU 绘图中完成此操作?
谢谢