你能用"R"通过参考吗?例如,在以下代码中:
setClass("MyClass",
representation(
name="character"
))
instance1 <-new("MyClass",name="Hello1")
instance2 <-new("MyClass",name="Hello2")
array = c(instance1,instance2)
instance1
array
instance1@name="World!"
instance1
array
Run Code Online (Sandbox Code Playgroud)
输出是
> instance1
An object of class “MyClass”
Slot "name":
[1] "World!"
> array
[[1]]
An object of class “MyClass”
Slot "name":
[1] "Hello1"
[[2]]
An object of class “MyClass”
Slot "name":
[1] "Hello2"
Run Code Online (Sandbox Code Playgroud)
但我希望如此
> instance1
An object of class “MyClass”
Slot "name":
[1] "World!"
> array
[[1]]
An object of class “MyClass”
Slot "name":
[1] "World!"
[[2]]
An object of class …Run Code Online (Sandbox Code Playgroud) 我们希望将数组中的所有值设置为负值.我尝试了很多东西,但还没有找到一个有效的解决方案.我想到了一个带有条件的for循环,但这似乎不起作用.
#pred_precipitation is our array
pred_precipitation <-rnorm(25,2,4)
for (i in nrow(pred_precipitation))
{
if (pred_precipitation[i]<0) {pred_precipitation[i] = 0}
else{pred_precipitation[i] = pred_precipitation[i]}
}
Run Code Online (Sandbox Code Playgroud)
谢谢你的提示!
我能以某种方式通过参考原子矢量使用子分配吗?
当然没有将它包装在1列data.table中使用:=.
library(data.table)
N <- 5e7
x <- sample(letters, N, TRUE)
X <- data.table(x = x)
upd_i <- sample(N, 1L, FALSE)
system.time(x[upd_i] <- NA_character_)
# user system elapsed
# 0.11 0.06 0.17
system.time(X[upd_i, x := NA_character_])
# user system elapsed
# 0.00 0.00 0.03
Run Code Online (Sandbox Code Playgroud)
如果R6可以提供帮助,那我就开放R6解决方案,因为它已经是我的一个解决方案了.
我已经检查过<-内部R6对象仍然会复制:gist.
我想写一个函数来反转任何数字的顺序.这是我的,但它不起作用.请帮我!
n=123
rev_number=function(n){
m=strsplit(as.character(n),"")
if (m==rev(m)) print("reversed number")
}
Run Code Online (Sandbox Code Playgroud)
期望的输出是 n=321
考虑这两个函数:
library(Rcpp)
cppFunction("NumericVector func1(NumericVector &x)
{
for (int i = 0; i < x.length(); i++)
x[i] = x[i] * 2;
return x;
}")
cppFunction("NumericVector func2(NumericVector x) // no &
{
for (int i = 0; i < x.length(); i++)
x[i] = x[i] * 2;
return x;
}")
Run Code Online (Sandbox Code Playgroud)
唯一的区别是,func1将其x作为参考参数,而func2将其作为值。如果这是常规的 C++,我会将其理解为func1允许更改调用代码中的值x,而这不会在func2.
然而:
> x <- 1:10/5 # ensure x is numeric, not integer
> x
[1] 0.2 0.4 0.6 0.8 …Run Code Online (Sandbox Code Playgroud) 在C++中,我们可以将变量声明为引用.
int a = 10;
int& b = a;
Run Code Online (Sandbox Code Playgroud)
如果我们设置b=15,a也会改变.
我想在Rcpp做类似的事情.
List X = obj_from_R["X"];
IntegerVector x_i = X[index];
x_i = value;
Run Code Online (Sandbox Code Playgroud)
我想X通过在其向量之一中插入一个值来更新R中的对象.上面的代码不起作用,所以我尝试了这个:
IntegerVector& x_i = X[index];
Run Code Online (Sandbox Code Playgroud)
并收到错误.
error: non-const lvalue reference to type 'IntegerVector'
(aka 'Vector<13>') cannot bind to a temporary of type 'Proxy' (aka 'generic_proxy<19>')
Run Code Online (Sandbox Code Playgroud) 这个问题关系到这个和这个。此处的区别在于,我没有传递Rcpp类型(例如NumericVector或)NumericMatrix,而是传递arma::sp_mat。
有什么方法可以将A传递sp_mat给C ++,修改其值,并使更改显示在R中的原始对象中吗?
可以使用来完成NumericMatrix,例如:
cppFunction("void frob(NumericMatrix& x)
{
for(NumericMatrix::iterator it = x.begin(); it != x.end(); ++it)
{
if(*it != 0) *it = *it + 5;
}
}")
M <- Matrix(0, 5, 1, sparse=TRUE)
M[1] <- 1.2345
m <- as.matrix(M)
frob(m)
m
#[,1]
#[1,] 6.2345
#[2,] 0.0000
#[3,] 0.0000
#[4,] 0.0000
#[5,] 0.0000
Run Code Online (Sandbox Code Playgroud)
相同的技术适用于arma::mat密集矩阵。但是对于稀疏矩阵,它不起作用:
cppFunction("void frob2(arma::sp_mat& x)
{
for(arma::sp_mat::iterator it = x.begin(); it != x.end(); …Run Code Online (Sandbox Code Playgroud) 我在Rcpp中遇到一个简单的代码问题.我的问题是我想通过将一个向量传递给一个函数来改变它.一个例子是:
//[[Rcpp::export]]
void ones(IntegerVector x, int lx){
int i;
for(i = 0; i < lx; i++){
x(i) = 1;
}
}
Run Code Online (Sandbox Code Playgroud)
我在R做的时候:
x = rep(-1, 10)
ones(x, length(x))
Run Code Online (Sandbox Code Playgroud)
向量x不会改变.我怎么能解决这个问题?
编辑:如果我传递x作为&x如何更改它的值?
编辑:尝试前两个答案中提出的两个方法后没有任何改变.
编辑:重新启动Rstudio,现在它的工作原理.......这是Rstudio用户的常见问题吗?
r ×7
rcpp ×5
armadillo ×1
c++ ×1
data.table ×1
for-loop ×1
if-statement ×1
oop ×1
r6 ×1
rcpp11 ×1