- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
借助 Rcpp,我在 C++ 中定义了一个矩阵 M。使用 M.nrow()
,我们应该能够检索行数。但是,当我尝试将行数作为 IntegerVector
返回时,答案不正确:
set.seed(1100)
M = matrix(sample(18), nrow = 6)
Rcpp::cppFunction('IntegerVector tccp5(IntegerMatrix M) { int x = M.nrow(); return x;}')
tccp5(M)
# [1] 0 0 0 0 0 0
正确答案应该是行数,例如
# [1] 6
你能解释一下发生了什么吗?
最佳答案
虽然@gfgm 的回答是正确的,但我想扩展为什么声明为 int
而不是 IntegerVector
会导致正确的构造...
特别是,每个返回 R 的 return 都使用 wrap()
无缝转换为 SEXP
,或指向该结构的 S 表达式。通过提供 IntegerVector
并返回一个 int
,wrap()
必须实例化一个长度为 的新
因为 IntegerVector
xint
下没有 SEXP
。另一方面,当定义的返回类型为 int
时,Rcpp 的 wrap()
特性能够正确地强制转换 int
转换为 IntegerVector
。
为了强调发生的底层“无缝”转换,让我们将参数 verbose = TRUE
添加到 cppFunction()
以查看 C++ 代码是:编译、链接并导入到 R 中。 (注意:我已经截断了编译的输出。)
如果我们考虑:
Rcpp::cppFunction('IntegerVector tccp5(IntegerMatrix M) { int x = M.nrow(); return x;}',
verbose = TRUE)
我们得到:
Generated code for function definition:
--------------------------------------------------------
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
IntegerVector tccp5(IntegerMatrix M) { int x = M.nrow(); return x;}
Generated extern "C" functions
--------------------------------------------------------
#include <Rcpp.h>
// tccp5
IntegerVector tccp5(IntegerMatrix M);
RcppExport SEXP sourceCpp_7_tccp5(SEXP MSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< IntegerMatrix >::type M(MSEXP);
rcpp_result_gen = Rcpp::wrap(tccp5(M));
return rcpp_result_gen;
END_RCPP
}
相比于:
Rcpp::cppFunction('int tccp5(IntegerMatrix M) { int x = M.nrow(); return x;}',
verbose = TRUE)
给出:
Generated code for function definition:
--------------------------------------------------------
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
int tccp5(IntegerMatrix M) { int x = M.nrow(); return x;}
Generated extern "C" functions
--------------------------------------------------------
#include <Rcpp.h>
// tccp5
int tccp5(IntegerMatrix M);
RcppExport SEXP sourceCpp_9_tccp5(SEXP MSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< IntegerMatrix >::type M(MSEXP);
rcpp_result_gen = Rcpp::wrap(tccp5(M));
return rcpp_result_gen;
END_RCPP
}
关于r - Rcpp 中 C++ 中的 .nrow() 方法返回零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49422523/
我有两个数据框。一个是 ~133 行,一个是 ~4337 行。它们每个都有两列包含相同类型的信息。第一列中的太阳高度和第二列中的辐射。我想将大数据框的行数减少到小数据框的行数,以便我可以继续进行分析而
假设: df['Column_Name'].max() # is the maximum value in a particular column in a dataframe 然后,您想要选择特定列
我有使用 R 的作业,但有一个小问题。在分配中,必须生成具有随机行数的几个矩阵,然后用于各种计算。一切都很完美,除非行数为 1。 在计算中,我以不同的方式使用 nrow(matrix),例如 if (
更新 使用在整个站点中找到的不同解决方案: 我仍然无法使用堆栈和 ldply 函数实现所需的输出: 所需的输出如下所示: Dataset Samples 1 WG
我正在尝试使用 for 循环创建一堆 ggplot2 图,然后将它们保存在多页 pdf 文档中,但我在使用 marrangeGrob 时遇到了问题。下面是一些示例代码: Plots = n is n
我有一个数据框列表,大小不一。我想删除行数较少的行(比方说 3, listdfs) [[1]] y1 y2 1 1 4 2 2 5 3 3 6 4 9 7 [[2]] y1
我想做什么? pd.read_csv(... nrows=###) 可以读取文件的前 n 行。我想在使用 pd.read_hdf(...) 时做同样的事情。 问题是什么? 我对 documentati
在一次练习中,我尝试使用 for 循环创建一个乘法表。我是编程新手,R 是我学习的第一门语言,所以我想知道循环内的哪些函数更快、更高效。目前,我没有使用 apply 系列的方法,因为我认为了解循环等基
如何从每个组的行数不同的组中绘制 n 行? df % group_by(color) %>% sample_n(nrow(.), replace = TRUE) outdat 但这会返回一个
我是一名学生,我正在努力让最后一段代码完成我的作业。这个问题已被问过一百万次,但我一直无法找到适合我的解决方案。当我运行这个程序时,我只能得到要打印的数组的最后一个位置。我无法使用任何快捷方式( ve
我想匹配/连接两个矩阵,一个带有值的小矩阵应该通过 rownames/colnames 匹配一个更大的矩阵。我只找到this回答。但是,我无法将位置匹配为代码行 frn full_matrix
我可以通过以下方式轻松地向数据框中添加一个空行; if(nrow(df)==0){ df[nrow(df)+1,] <- NA } 如何对 data.table 做同样的事情? 最佳答案 这似乎可行
借助 Rcpp,我在 C++ 中定义了一个矩阵 M。使用 M.nrow(),我们应该能够检索行数。但是,当我尝试将行数作为 IntegerVector 返回时,答案不正确: set.seed(1100
我是新来的 R ,所以这很可能是一个愚蠢的问题。 每次我创建人工数据,有时使用导入的数据集,R告诉我我的变量没有行或列。 我可以运行回归,但我不能根据我的变量具有的行/列数来执行命令。 例如,假设我有
我有一个功能: extract_tmp <- function(x,y) { nr <- nrow(x$y) ind <- seq(6, nr, by = 12) out <- lappl
我正在尝试使用pandas pd.read_csv函数中的skiprows,但是当我使用它时,我只能从它应该开始的位置获取该特定行。 即Skiprows=1,将仅输出第 1 行。 知道为什么会发生这种
我的功能有问题 grid.arrange .我想绘制一个数据集的箱线图,并在下面绘制一个带有最小值、平均值、中值和最大值的数据表。我收到以下错误消息:nrow * ncol >= n is not T
我对 C 编程完全陌生(只做 java),这些语句的不同足以让我感到困惑。我想知道我的方法头是否声明我需要在方法内声明 Nrows 和 Ncols (来 self 的主方法)?只需将它们设置为某个变量
我正在尝试学习如何向开源项目提交 Pull-Request。所以我选择了issue #23455来自 Pandas 开发者。这是一个简单的文档错误。但是我意识到我不知道 nrows 在 from_re
我遇到了一个非常奇怪的问题。我正在尝试从 excel 文件中读取一些数据,但属性 nrows 的值有误。虽然我的文件有很多行,但它只返回 2。 我在 pydev eclipse 中工作。我不知道到底是
我是一名优秀的程序员,十分优秀!