rcpp中%的糖
有%的Rcpp糖吗 例如,我在R中有以下陈述rcpp中%的糖,r,rcpp,R,Rcpp,有%的Rcpp糖吗 例如,我在R中有以下陈述 y <- c('XA','XB','XC','XF','XK','XL','XM','XN','XO','XP','XS','XU','XW','XY', 'DF','DS','AS','XL','FG') x <- ifelse(y %in% c("XA","XB","XC","XF","XK","XL","XM","XN","XO","XP","XS","XU","XW","XY"),"KCA","KUS") 我可以在Rcpp中使用
y <- c('XA','XB','XC','XF','XK','XL','XM','XN','XO','XP','XS','XU','XW','XY', 'DF','DS','AS','XL','FG')
x <- ifelse(y %in% c("XA","XB","XC","XF","XK","XL","XM","XN","XO","XP","XS","XU","XW","XY"),"KCA","KUS")
我可以在Rcpp中使用,或者我如何在Rcpp中使用| |来避免错误?请查看,以获取在运算符中糖化的的示例。具体而言,这里给出的示例是:
CharacterVector A = CharacterVector::create("a", "b", "c", "c", "e", "b", "d");
CharacterVector B = CharacterVector::create("a", "b");
LogicalVector C = in(A, B);
在您的情况下,我们可以构建:
#include <Rcpp.h>
// [[Rcpp::export]]
Rcpp::CharacterVector my_classify(Rcpp::CharacterVector x,
Rcpp::CharacterVector table,
std::string true_cond = "KCA",
std::string false_cond = "KUS") {
Rcpp::CharacterVector out = Rcpp::CharacterVector(x.size());
Rcpp::LogicalVector cond = in(x, table);
for(unsigned int i = 0; i < cond.size(); ++i){
if(cond[i]) {
out[i] = true_cond;
} else {
out[i] = false_cond;
}
}
return out;
}
我认为在%中执行<代码> %>的C++方式是 STD::FIN()/<代码>,也可以参见,欢迎使用C++和字符串。您使用了一个“
,字符串需要的是”
。之后,您的==
比较运算符代码应该可以工作。是的,在将“
替换为”
后,它就可以工作了,但为什么会这样呢?这是否意味着我不能在std::vector
中使用”
?返回到C的语言定义。“
用于单个字符,它也强制转换为整数,因此您看到了错误。所需字符串“
。要在%
中真正复制%,需要查看源代码。在%
中键入%in%(周围带单引号)显示%
中的%本身就是匹配的糖。这是的主要功能。旧问题,但这也可以在中找到。doxygen文档缺少示例,很难导航。我不确定是否会链接到那里。肯定同意。同样,我发现示例(Dirk等人经常引用。)阅读起来很痛苦,需要更多的阅读。我认为这可能是一个限制,因为R目前采用的文档风格。但即使如此,我觉得它可能会更好,特别是当有这么多奇妙的功能可以让人的生活更轻松时(直到一个人进入std)
ambiguous overload for operator'=='(operand types are 'std::basic_string<char>' and 'int')
%in%
CharacterVector A = CharacterVector::create("a", "b", "c", "c", "e", "b", "d");
CharacterVector B = CharacterVector::create("a", "b");
LogicalVector C = in(A, B);
#include <Rcpp.h>
// [[Rcpp::export]]
Rcpp::CharacterVector my_classify(Rcpp::CharacterVector x,
Rcpp::CharacterVector table,
std::string true_cond = "KCA",
std::string false_cond = "KUS") {
Rcpp::CharacterVector out = Rcpp::CharacterVector(x.size());
Rcpp::LogicalVector cond = in(x, table);
for(unsigned int i = 0; i < cond.size(); ++i){
if(cond[i]) {
out[i] = true_cond;
} else {
out[i] = false_cond;
}
}
return out;
}
x = c('XA','XB','XC','XF','XK','XL','XM','XN','XO','XP','XS',
'XU','XW','XY', 'DF','DS','AS','XL','FG')
table = c("XA","XB","XC","XF","XK","XL","XM","XN",
"XO","XP","XS","XU","XW","XY")
y = my_classify(x, table)
y
#> [1] "KCA" "KCA" "KCA" "KCA" "KCA" "KCA" "KCA" "KCA" "KCA" "KCA" "KCA"
#> [12] "KCA" "KCA" "KCA" "KUS" "KUS" "KUS" "KCA" "KUS"