小编MYa*_*208的帖子

R中多个ifelse的替代方案

我厌倦了多个ifelse陈述R.我知道有一个名称的功能,switch但如果以下示例,无法弄清楚如何使用.

set.seed(12345)
Y <- runif(n=100, min=0, max=50)

ifelse(
    test = Y < 5
  , yes  = "A"
  , no   =  ifelse(
         test = Y < 10
       , yes  = "B"
       , no = "C"
       )
    )
Run Code Online (Sandbox Code Playgroud)

产量


  [1] "C" "C" "C" "C" "C" "B" "C" "C" "C" "C" "A" "B" "C" "A" "C" "C" "C" "C"
 [19] "B" "C" "C" "C" "C" "C" "C" "C" "C" "C" "C" "C" "C" "A" "B" "C" "C" …
Run Code Online (Sandbox Code Playgroud)

if-statement r

3
推荐指数
1
解决办法
1375
查看次数

在knitr中抑制错误消息

我想知道如何抑制错误信息knitr.我的MWE如下:

\documentclass{article} 
\begin{document}
<< Test >>=
1:10
X
@ 
\end{document}
Run Code Online (Sandbox Code Playgroud)

编辑

该对象X不存在.我想X在我的代码块中显示并想要评估它,即使这会引发错误.但是我不想在我的.tex文档中显示任何错误,因为我们可以通过设置来抑制警告warning=FALSE.

latex r sweave knitr

3
推荐指数
1
解决办法
2631
查看次数

在knnr中使用Rnw中的ggvis

我想知道我是否可以在.Rnw中使用ggvis和knitr.我尝试了下面的代码RStudio Version 0.98.1091.但它没有用.

\documentclass{book}
\usepackage[T1]{fontenc}
\usepackage{float}
\usepackage{booktabs}
\usepackage{dcolumn}
\usepackage{geometry}
\geometry{verbose,tmargin=2cm,bmargin=2cm,lmargin=2cm,rmargin=2cm}

\begin{document}
\chapter{Test}
\begin{figure}[H]
<< label = Plot1, fig.lp = "Plot1", fig.cap = "Test Plot" >>=
library(ggvis)
p <- mtcars %>% ggvis(x = ~wt, y = ~mpg) %>% layer_points()
print(p)     # Commenting this line will compile the document
@
\end{figure}
\end{document}
Run Code Online (Sandbox Code Playgroud)

它会引发以下错误:

LaTeX errors:
! Missing $ inserted.
<inserted text> 
                $
l.70 \end{kframe}<!--html_
                          preserve--><div id="plot_id298740869-container" cl...
! Please use \mathaccent for accents in math mode.
Run Code Online (Sandbox Code Playgroud)

任何帮助将受到高度赞赏.谢谢

编辑

注释该行将print(p) …

latex r rstudio knitr ggvis

3
推荐指数
1
解决办法
1186
查看次数

从 ggplot2 图中删除右边框

使用以下代码,我可以删除顶部和右侧边框以及其他内容。我想知道如何ggplot2仅删除图形的右边框。

p <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() 

p + theme_classic()
Run Code Online (Sandbox Code Playgroud)

plot r ggplot2 r-grid

3
推荐指数
1
解决办法
1955
查看次数

通过boot.ci函数获取多个统计信息的Bootstrap Confidence Intervals

我希望通过boot.ci函数获得多个统计信息的bootstrap置信区间.这是我的MWE.

我有两个统计信息,out并希望找到这两个统计信息的bootstrap置信区间.但是,boot.ci函数仅为第一个统计量(t1*)提供自举置信区间,但不为第二个统计量(t2*)提供自举置信区间.

set.seed(12345)
df <- rnorm(n=10, mean = 0, sd = 1)


Boot.fun <- 
  function(data, idx) {
    data1 <- sample(data[idx], replace=TRUE)
    m1 <- mean(data1)
    sd1 <- sd(data1)
    out <- cbind(m1, sd1)
    return(out)
  }

Boot.fun(data = df)

library(boot)
boot.out <- boot(df, Boot.fun, R = 20)
boot.out

RDINARY NONPARAMETRIC BOOTSTRAP


Call:
  boot(data = df, statistic = Boot.fun, R = 20)


Bootstrap Statistics :
  original     bias    std. error
t1* -0.4815861  0.3190424   0.2309631
t2*  0.9189246 -0.1998455   0.2499412

boot.ci(boot.out=boot.out, …
Run Code Online (Sandbox Code Playgroud)

r statistics-bootstrap

3
推荐指数
1
解决办法
1884
查看次数

kableExtra:表格脚注中的超链接

我看了这个问题,但给定的方法似乎不适用于脚注(参见 MWE)。我想知道如何使用 package.json 在表格脚注中添加超链接kableExtra

knitr::kable(
              x       = mtcars[1:4, 1:5]
            , format  = "latex"
            , caption = "Table Caption with hyperlink[note]"
            , escape  = FALSE
            ) %>%
  kableExtra::add_footnote("\\href{https://github.com/haozhu233/kableExtra}{kableExtra}")
Run Code Online (Sandbox Code Playgroud)

r knitr kableextra kable

3
推荐指数
1
解决办法
605
查看次数

dplyr:使用多边形函数生成多项式系数

我想将多项式系数附加到data.frame,如下所示:

df1 <- 
  structure(list(
    Y = c(4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 16, 16, 16, 
          16, 16, 32, 32, 32, 32, 32, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 16, 
          16, 16, 16, 16, 32, 32, 32, 32, 32, 4, 4, 4, 4, 4, 8, 8, 8, 8, 
          8, 16, 16, 16, 16, 16, 32, 32, 32, 32, 32)), 
    class = "data.frame", row.names = c(NA, -60L))

library(tidyverse)
df1 %>%
  dplyr::mutate(
    Linear    = poly(x …
Run Code Online (Sandbox Code Playgroud)

r dplyr tidyverse poly

3
推荐指数
1
解决办法
194
查看次数

在R中的levelplot上显示数据值

我想知道是否可以在R中的 levelplot(网格包)上显示数据值.如果有人帮我这样做,我会很感激.提前致谢.

graphics r lattice

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

用ggdendro在树形图的片段下显示变量标签

我的问题与Andrie 对我之前问题的回答有关.我的问题是,是否可以在树形图的相应部分下显示变量标签和汽车标签?

library(ggplot2)
library(ggdendro)
data(mtcars)
x <- as.matrix(scale(mtcars))
dd.row <- as.dendrogram(hclust(dist(t(x))))
ddata_x <- dendro_data(dd.row)
p2 <- ggplot(segment(ddata_x)) +
geom_segment(aes(x=x0, y=y0, xend=x1, yend=y1))
print(p2)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

r ggplot2 ggdendro

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

knitr:不同目录中的子文档

如果子文档位于同一目录中,我可以使用以下代码.

<<child-demo, child=knitr-input-child.Rnw, eval=TRUE>>=
@
Run Code Online (Sandbox Code Playgroud)

我想知道如果它不在主文档的同一目录中,如何使用子文档.在此先感谢您的帮助和时间.

r sweave knitr

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