为了对我的问题进行语境化,我使用的Matrix类具有以下定义:
Matrix(unsigned int, unsigned int); // matrix of the given dimension full of zeroes
Matrix(Matrix*); // creates a new matrix from another one
int &operator()(int, int); // used to access the matrix
int **matrix; // the matrix
Run Code Online (Sandbox Code Playgroud)
现在拿这两个代码片段:
第一:
Matrix test(4,4);
Matrix ptr = test;
ptr(0,0) = 95;
Run Code Online (Sandbox Code Playgroud)
第二:
Matrix test(4,4);
Matrix *ptr = &test;
(*ptr)(0,0) = 95;
Run Code Online (Sandbox Code Playgroud)
两个代码都有相同的效果,(0,0)位置的元素接收95(第一个代码片段与Java非常相似,这是导致我提出这个问题的原因).问题是,两种方式都正确地分配了对象吗?
data.tabel的引用分配似乎有一个细微的差别:=标准中的=功能形式.
标准形式强制RHS到矢量,功能形式没有.一个细节,但没有记录,因为我相信.
library(data.table)
dt <- data.table(a = c('a','b','c'))
v <- c('A','B','C')
l <- list(v)
all.equal(copy(dt)[, new := v], copy(dt)[, `:=` (new = v)])
# [1] TRUE
all.equal(copy(dt)[, new := l], copy(dt)[, `:=` (new = l)])
# [1] "Datasets have different column modes. First 3: new(character!=list)"
copy(dt)[, new := l][]
# a new
# 1: a A
# 2: b B
# 3: c C
copy(dt)[, `:=` (new = l)][]
# a new
# 1: a A,B,C
# 2: b A,B,C …Run Code Online (Sandbox Code Playgroud) 为环境中的名称赋值和设置变量的环境有什么区别?我无法从文档中找到它.
例如:
MyTestFunc = function(x)
{
myVal = "abcde"
# what is this doing? why is myVal not in the global environment after
# this call? I see it adds an environment attribute to the variable,
# but what good is that?
environment(myVal) = globalenv()
assign( "myVal" , myVal , envir = globalenv() )
# this seems to copy graphics:::rect to the current environment which seems
# to contradict the behavior of environment( myVal ) above
environment( rect ) = …Run Code Online (Sandbox Code Playgroud) 在Matlab中,分配单元阵列到一个结构数组字段foo是可能的
my_array(1000).foo = [];
[my_array.foo] = some_cell{:};
Run Code Online (Sandbox Code Playgroud)
现在我想要做的是为数组中的所有字段分配一个值.但无论我尝试什么,Matlab都会返回错误消息,而不是默默地假设如果我想分配一个大小的单个元素[1x1],它应该被分配给所有字段.如果我可以简单地说例如:我会很高兴:
my_array.foo = pi;
??? Incorrect number of right hand side elements in dot name assignment.
Missing [] around left hand side is a likely cause.
Run Code Online (Sandbox Code Playgroud)
那么,如何将单个值分配给整个结构数组中的字段?
vector(以及list其他容器)具有成员函数(MF)assign.我想比较assignMF(范围版本)与赋值运算符.
据我所知,在以下情况下使用是有用的assign:
在其他情况下,assignMF 没有缺点,可以使用赋值运算符.我对吗?使用assignMF 还有其他一些原因吗?
昨天我给出了这个答案:将数据表匹配五列以更改另一列中的值.
在评论中,OP询问我们是否能够有效地实现两个表的左连接,从而获得将导致右表分配给左表的NA.在我看来,data.table并没有提供任何这样做的方法.
以下是我在该问题中使用的示例案例:
set.seed(1L);
dt1 <- data.table(id=1:12,expand.grid(V1=1:3,V2=1:4),blah1=rnorm(12L));
dt2 <- data.table(id=13:18,expand.grid(V1=1:2,V2=1:3),blah2=rnorm(6L));
dt1;
## id V1 V2 blah1
## 1: 1 1 1 -0.6264538
## 2: 2 2 1 0.1836433
## 3: 3 3 1 -0.8356286
## 4: 4 1 2 1.5952808
## 5: 5 2 2 0.3295078
## 6: 6 3 2 -0.8204684
## 7: 7 1 3 0.4874291
## 8: 8 2 3 0.7383247
## 9: 9 3 3 0.5757814
## 10: 10 1 4 -0.3053884
## 11: …Run Code Online (Sandbox Code Playgroud) 这很简单,但我已经搜索并未能找到解决这个小问题的方法。
我想使用函数的参数作为新数据框的名称,例如:
assign.dataset<-function(dataname){
x<-c(1,2,3)
y<-c(3,4,5)
dataname<<-rbind(x,y)
}
Run Code Online (Sandbox Code Playgroud)
然后
assign.dataset(new.dataframe.name)
Run Code Online (Sandbox Code Playgroud)
只需创建一个名为dataname的新数据集。
我曾尝试使用粘贴和分配功能,但没有成功。
非常感谢
我有一个非常大的功能,需要几个小时才能给我结果.我忘了给它起个名字.有什么方法可以显示我的功能结果吗?
提前致谢.
我会实现一个简单的链表。这是我到目前为止的(工作)代码:
pub struct LinkedList<T> {
start: Option<Box<Link<T>>>,
}
impl<T> LinkedList<T> {
pub fn new() -> LinkedList<T> {
return LinkedList { start: None };
}
}
struct Link<T> {
value: Box<T>,
next: Option<Box<Link<T>>>,
}
impl<T> Link<T> {
fn new_end(value: T) -> Link<T> {
return Link::new(value, None);
}
fn new(value: T, next: Option<Box<Link<T>>>) -> Link<T> {
return Link {
value: Box::new(value),
next,
};
}
}
Run Code Online (Sandbox Code Playgroud)
列表中的下一个是附加到列表的方法;这就是我想出的:
pub fn append(&mut self, element: T) {
// Create the link to append
let new_link = …Run Code Online (Sandbox Code Playgroud) 刚开始使用Haskell,据说除了IO包外,Haskell中的所有内容都是“不可变的”。因此,当我将名称绑定到某物时,它总是不可变的?问题,如下:
Prelude> let removeLower x=[c|c<-x, c `elem` ['A'..'Z']]
Prelude> removeLower "aseruiiUIUIdkf"
"UIUI"
Run Code Online (Sandbox Code Playgroud)
所以在这里:
1. “removeLower" is an immutable? Even it's a function object?
But I can still use "let" to assign something else to this name.
2. inside the function "c<-x" seems that "c" is a variable.
It is assigned by list x's values.
Run Code Online (Sandbox Code Playgroud)
我使用的是 C 语言中的“变量”这个词,不知道 Haskell 是如何命名它的所有名称的?
谢谢。