我有一个矢量说
c(1,1,1,1,1,1,2,3,4,5,7,7,5,7,7,7)
Run Code Online (Sandbox Code Playgroud)
如何找到每个元素的计数并返回3个最常出现的元素,即1,7,5?
我认为这应该很简单,但我遇到了麻烦.
通过使用xtable和knitr,我将一个表添加到我的RMD文档并导出为PDF文件.
```{r, results='asis'}
library(xtable)
xtable(matrixtable)
````
Run Code Online (Sandbox Code Playgroud)
除了有一条线外,它看起来很棒
% latex table generated in R 3.1.0 by xtable 1.7-3 package % Wed Jun 25 13:34:57 2014
Run Code Online (Sandbox Code Playgroud)
如何删除此行.我试着设置message=FALSE但它不起作用.
我们知道Rf_error()在Rcpp中应该避免调用,因为它涉及堆栈上的C++析构函数的longjmp.这就是为什么我们宁愿在Rcpp代码中抛出C++异常(喜欢throw Rcpp::exception("...")或通过stop("...")函数).
但是,R警告也可能导致调用Rf_error()(此行为取决于warn选项).所以,打电话Rf_warning()也是有风险的.
Rcpp::sourceCpp(code = '
#include <Rcpp.h>
using namespace Rcpp;
class Test {
public:
Test() { Rcout << "start\\n"; }
~Test() { Rcout << "end\\n"; }
};
// [[Rcpp::export]]
void test() {
Test t;
Rf_warning("test");
}
')
options(warn=10)
test()
## start
## Error in test() : (converted from warning) test
Run Code Online (Sandbox Code Playgroud)
我们看到析构函数没有被调用(没有"结束"消息).
如何用C++生成R警告 - 对析构函数友好的方式?
有没有办法在不导入文件的情况下获取文件中的行数?
到目前为止,这就是我正在做的事情
myfiles <- list.files(pattern="*.dat")
myfilesContent <- lapply(myfiles, read.delim, header=F, quote="\"")
for (i in 1:length(myfiles)){
test[[i]] <- length(myfilesContent[[i]]$V1)
}
Run Code Online (Sandbox Code Playgroud)
但由于每个文件都很大,所以太耗费时间.
我想检查一个R函数的"..."(省略号)参数是否已经输入了一些值/参数.
目前我正在使用类似的东西:
test1 <- function(...) {
if (missing(...)) TRUE
else FALSE
}
test1()
## [1] TRUE
test1(something)
## [2] FALSE
Run Code Online (Sandbox Code Playgroud)
它有效,但?missing不表明该方式是否正确/有效.
如果以上不正确,那么这样做的方法是什么?或者还有其他更快的方法?PS.我需要针对此问题进行此类验证.
是否有可能删除所有的标点符号,但保留表情符号如
:-(
:)
:d
:p
structure(list(text = structure(c(4L, 6L, 1L, 2L, 5L, 3L), .Label = c("ãããæããããéãããæãããInappropriate announce:-(",
"@AirAsia your direct debit (Maybank) payment gateways is not working. Is it something you are working to fix?",
"@AirAsia Apart from the slight delay and shortage of food on our way back from Phuket, both flights were very smooth. Kudos :)",
"RT @AirAsia: ØØÙØÙÙÙÙ ÙØØØ ØØØÙ ÙØØØØÙ ØØØØÙÙÙí í Now you can enjoy a #great :D breakfast onboard with our new breakfast meals! …Run Code Online (Sandbox Code Playgroud) 我有一个数据集,其中包含在聊天会话期间创建的两个人之间的对话框.例如,
我想在R中创建一个简单的函数,它将在B说出一行之前组合A的行,这样我就有了一个看起来像这样的数据集:
我知道如何合并/组合单元格,但我不知道如何创建一个逻辑语句创建一个指示符A在B之前说话(反之亦然).
出于某些原因,我想以更多的Lisp/Scheme方式使用R调用(至少就语法而言)(我们都知道R 受到了Scheme的大量启发).
因此,我设置了以下功能:
. <- function(f, ...)
eval(match.call()[-1], envir=parent.frame())
Run Code Online (Sandbox Code Playgroud)
这允许我表达例如以下R代码:
x <- sort(sample(1:10, 5, replace=TRUE))
for (i in x) {
print(1:i)
}
Run Code Online (Sandbox Code Playgroud)
在以下语义等效形式中:
.(`<-`, x,
.(sort,
.(sample,
.(`:`, 1, 5),
5, replace=TRUE)))
.(`for`, i, x,
.(`{`,
.(print,
.(`:`, 1, i))))
Run Code Online (Sandbox Code Playgroud)
我对目前的定义非常满意.(因为它只是为了娱乐而制作).但它肯定远非完美.特别是它的表现当然很差:
microbenchmark::microbenchmark(1:10, .(`:`, 1, 10))
## Unit: nanoseconds
## expr min lq median uq max neval
## 1:10 189 212.0 271.5 349 943 100
## .(`:`, 1, 10) 8809 10134.5 10763.0 11467 44066 100
Run Code Online (Sandbox Code Playgroud)
所以我想知道你是否可以提出一些有关定义的想法.来解决上述问题.欢迎使用C/C++代码.
好的,我知道答案,但受到这个问题的启发,我想对以下内容得到一些好的看法:为什么下面的Rcpp练习是ca. 比内置的快15%(对于长向量)exp()?我们都知道Rcpp是R/C API的包装器,所以我们应该期待性能略差一些.
Rcpp::cppFunction("
NumericVector exp2(NumericVector x) {
NumericVector z = Rcpp::clone(x);
int n = z.size();
for (int i=0; i<n; ++i)
z[i] = exp(z[i]);
return z;
}
")
library("microbenchmark")
x <- rcauchy(1000000)
microbenchmark(exp(x), exp2(x), unit="relative")
## Unit: relative
## expr min lq median uq max neval
## exp(x) 1.159893 1.154143 1.155856 1.154482 0.926272 100
## exp2(x) 1.000000 1.000000 1.000000 1.000000 1.000000 100
Run Code Online (Sandbox Code Playgroud) 我正在编写一个适用于大型数据集和其他相关计算的机器学习程序.由于数据集可能非常大,因此某些计算会产生非常大的矩阵(例如29,000 x 29,000 Array {Float64,2}),并且它们需要大量存储(RAM).在程序的后面,不再需要一些元素(如初始数据集),但它们仍在浪费内存空间.
有没有办法在某个时刻"释放"变量?或者,有没有办法分享一些硬盘部分,如交换空间?