小编TKN*_*TKN的帖子

将算术运算符视为函数

我已经读过R中的所有东西都是功能.所以我想知道"+"是否也是一个函数,如果我们可以写出类似的东西:

xx <- c(1,2,3)
yy <- c(1,2,3,4,5,6)

# zz is the sum of the two lengths
zz <- +(if(exists("xx")) length(xx), if(exists("yy")) length(yy))
Run Code Online (Sandbox Code Playgroud)

syntax r function

14
推荐指数
1
解决办法
529
查看次数

在rmarkdown/knitr中使用R代码创建附录

是否可以在附录中获取所有代码.假设我在文档中有两个块,然后是一些文本.

```{r, echo=TRUE}
x <- 4+5
x
```  
Above is X output.

```{r, echo=TRUE}
y <- 22+325
y
```   

Above is Y output.
Run Code Online (Sandbox Code Playgroud)

然后我想要一个附录中的所有代码,但显示好像我放入eval=FALSE了大块.

像这样的东西

```{r, SHOW_ALL_CODE=TRUE}
```
Run Code Online (Sandbox Code Playgroud)

预期产量:

Chunk_1  
y <- 22+325  
y

Chunk_2  
x <- 4+5  
x
Run Code Online (Sandbox Code Playgroud)

r knitr r-markdown

12
推荐指数
3
解决办法
8810
查看次数

与randomForest相比,游侠的错误预测

我正在尝试使用rangerR软件包来加速进行大量randomForest计算.我正在检查我从中得到的预测,并注意到一些有趣的事情,因为预测完全取消了.

下面是一个可重复的例子比较randomForestranger.

data(iris)
library(randomForest)


iris_spec <- as.factor(iris$Species)
iris_dat <- as.matrix(iris[, !(names(iris) %in% "Species")])

set.seed(1234)

test_index <- sample(nrow(iris), 10)
train_index <- seq(1, nrow(iris))[-test_index]


iris_train <- randomForest(x = iris_dat[train_index, ], y = iris_spec[train_index], keep.forest = TRUE)
iris_pred <- predict(iris_train, iris_dat[test_index, ])

iris_train$confusion


##            setosa versicolor virginica class.error
## setosa         47          0         0  0.00000000
## versicolor      0         42         3  0.06666667
## virginica       0          4        44  0.08333333


cbind(as.character(iris_pred), as.character(iris_spec[test_index]))
##       [,1]         [,2]        
##  [1,] "setosa"     "setosa" …
Run Code Online (Sandbox Code Playgroud)

r random-forest

11
推荐指数
1
解决办法
4367
查看次数

更改ggplot图例标题

所以这是我的ggplot的代码.如何最简单地更改图例的标题?我知道我可以将gg_group变量更改为my_title <- c(rep("train",10), rep("validation", 10)).但我想将标题更改为" whatever I want"而不更改任何变量.

library(ggplot2)
y <- c(rnorm(10,1), rnorm(10,3))
x <- rep(seq(1,10,1),2)
gg_group <- c(rep("train",10), rep("validation", 10))

gg_data <- data.frame(y=y, x=x, gg_group=gg_group)

p <- ggplot(gg_data, aes(x=x, y=y, group=gg_group))
p + geom_line(aes(colour=gg_group))
Run Code Online (Sandbox Code Playgroud)

我也试过这段代码:

p + geom_line(aes(colour=gg_group)) + scale_shape_discrete(name="Dataset",labels=c("Train", "Validation"))

但这不起作用.*编辑,检查来自Jaap和JasonAizkalns的优秀snwer.

r legend title ggplot2

7
推荐指数
2
解决办法
7691
查看次数

正则表达式删除<>之间的所有内容

我正在学习网络搜索.我掌握了一堆数据但结构凌乱.
我有一个这种形式的字符串向量:
"9,55< U+00A0>x< U+00A0>1016",(现在当我写它时,我认为它是一种特殊的语法,因为我不能在此处粘贴它而不在"U"之前放置一个空格)我在网站上搜索被写成"9,55*10 ^ 16".

从长远来看,我的目标是将此字符串转换为数字变量,即95500000000000000.但首先,我要删除第一个"<"和最后一个">"之间的所有内容.以下是我的尝试.

gsub("<(.*?)>", "", vectorOfStrings)
Run Code Online (Sandbox Code Playgroud)

编辑:字符串最好在R中使用"9,55\U{00A0}x\U{00A0}1016",因为"<"和">"不是字符串中的实际文字.

regex r gsub web-scraping

2
推荐指数
1
解决办法
839
查看次数

标签 统计

r ×5

function ×1

ggplot2 ×1

gsub ×1

knitr ×1

legend ×1

r-markdown ×1

random-forest ×1

regex ×1

syntax ×1

title ×1

web-scraping ×1