更新:自问这个问题以来,dplyr已经更新,现在按照OP的要求执行
我正尝试获得第二至第七行中data.frame使用dplyr.
我这样做:
require(dplyr)
df <- data.frame(id = 1:10, var = runif(10))
df <- df %>% filter(row_number() <= 7, row_number() >= 2)
Run Code Online (Sandbox Code Playgroud)
但这会引发错误.
Error in rank(x, ties.method = "first") :
argument "x" is missing, with no default
Run Code Online (Sandbox Code Playgroud)
我知道我可以轻松制作:
df <- df %>% mutate(rn = row_number()) %>% filter(rn <= 7, rn >= 2)
Run Code Online (Sandbox Code Playgroud)
但我想明白为什么我的第一次尝试不起作用.
假设我在C++中有以下两个函数:
// [[Rcpp::export]]
SEXP foo() {
int a = 1;
Rcpp::XPtr<int> ptr(&a, true);
return ptr;
}
// [[Rcpp::export]]
int bar(SEXP a){
Rcpp::XPtr<int> x(a);
int b = *x;
return b;
}
Run Code Online (Sandbox Code Playgroud)
我希望能够在R中调用类似下面的内容.当然,在这个例子中我可以返回一个int到R in foo,但在我的原始代码中,a是一个有点复杂的数据结构,我不想只返回指针为此,它可以被另一个被调用的C++函数重用bar
a <- foo()
bar(a)
Run Code Online (Sandbox Code Playgroud)
在这个例子中我期望bar(a)返回1而不是0.我该如何解决这个问题?
我需要在列表中逐行写入矩阵和稀疏矩阵的文件,我正在做这样的事情:
#include <RcppArmadillo.h>
// [[Rcpp::export]]
bool write_rows (Rcpp::List data, Rcpp::CharacterVector clss, int n) {
int len = data.length();
for(int i = 0; i<n; i++) {
for(int j=0; j<len; j++) {
if (clss[j] == "matrix") {
Rcpp::NumericMatrix x = data[j];
auto row = x.row(i);
// do something with row i
} else if (clss[j] == "dgCMatrix") {
arma::sp_mat x = data[j];
auto row = x.row(i);
// do something different with row i
}
}
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
可以在R中调用此函数:
data <- …Run Code Online (Sandbox Code Playgroud) 我有一个rmarkdown文档,我正在将此文件转换为md文档.我的问题是我希望将绘图创建的图片放在文件的同一目录中名为Images的文件夹中.
假设我有这个文件:
---
title: "my test"
author: "daniel"
date: "18/08/2015"
output:
md_document
---
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this: …Run Code Online (Sandbox Code Playgroud) 如何从R重命名目录?
我试过了:
Warning message:
In file.rename(from = sprintf("content/%s-content", pu_name), to = sprintf("content/%s", :
cannot rename file 'content/pu.train2-content' to 'content/train2', reason 'Directory not empty'
Run Code Online (Sandbox Code Playgroud) src/例如src/libfoo,当我安装软件包时,子文件夹中的C或C ++代码不会被编译。
当我搜索了其他问题,我发现这是提到Makevars。我搜索了Matrix包Makevars。我认为我应该补充:
PKG_LIBS: -Llibfoo
Run Code Online (Sandbox Code Playgroud)
但这没有用。
我还发现在编写R Extensions上。我在我的Makevars中添加了以下内容,但它也不起作用。
SOURCES = $(libfoo/*.c)
OBJECTS = $(SOURCES:.c=.o)
Run Code Online (Sandbox Code Playgroud)
我应该如何调整Makevars文件?
我正在尝试使用以下方式dplyr:
假设你有,data.frame y并且你想根据的值创建一个新的变量var1
y <- data.frame(var1 = rnorm(100))
y$var2 <- 0
y$var2[y$var1 > 0.5] <- 1
Run Code Online (Sandbox Code Playgroud)
是否有可能使用dplyr's mutate和filter?
我在rmarkdown文档中创建了一些R练习,但我不希望显示答案,除非用户点击按钮.
我设法使用以下代码执行此操作:
---
title: "Aula 01 - Exercícios"
date : 2016-01-18
layout: post
comments: true
tags: exercicio
category: exercicio
---
<script>
var toggle = function(i) {
var mydiv = document.getElementById('q' + i);
if (mydiv.style.display === 'block' || mydiv.style.display === '')
mydiv.style.display = 'none';
else
mydiv.style.display = 'block'
}
</script>
1) Calcule o número de ouro no R.
$$ \frac{1 + \sqrt{5}}{2} $$
<div id="q1" style="display:none;" >
```{r}
(1 + sqrt(5))/2
```
</div>
<button type = "button" onclick="toggle(1);" class = "btn …Run Code Online (Sandbox Code Playgroud) 我正在尝试重现glmR中二项式的结果
考虑来自http://www.ats.ucla.edu/stat/r/dae/logit.htm的数据
mydata <- read.csv("http://www.ats.ucla.edu/stat/data/binary.csv")
Run Code Online (Sandbox Code Playgroud)
我可以使用以下模型拟合模型:
model <- glm(admit ~ gre + gpa + rank, data = mydata, family = "binomial")
Run Code Online (Sandbox Code Playgroud)
并且,仅使用对象重现模型:
model_r <- glm(as.numeric(model$y)~0+model.matrix(model), family = binomial)
cbind(coef(model), coef(model_r))
## [,1] [,2]
## (Intercept) -3.44954840 -3.44954840
## gre 0.00229396 0.00229396
## gpa 0.77701357 0.77701357
## rank -0.56003139 -0.56003139
Run Code Online (Sandbox Code Playgroud)
现在假设该列admit是该列中许多文章的成功数n:
mydata$n <- 1 + rbinom(n = 400, size = 2, prob = 0.5)
Run Code Online (Sandbox Code Playgroud)
现在我必须使用以下模型:
model <- glm(cbind(admit, n-admit) ~ gre + gpa …Run Code Online (Sandbox Code Playgroud)