我厌倦了多个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) 我想知道如何抑制错误信息knitr
.我的MWE如下:
\documentclass{article}
\begin{document}
<< Test >>=
1:10
X
@
\end{document}
Run Code Online (Sandbox Code Playgroud)
编辑
该对象X
不存在.我想X
在我的代码块中显示并想要评估它,即使这会引发错误.但是我不想在我的.tex
文档中显示任何错误,因为我们可以通过设置来抑制警告warning=FALSE
.
我想知道我是否可以在.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) …
使用以下代码,我可以删除顶部和右侧边框以及其他内容。我想知道如何ggplot2
仅删除图形的右边框。
p <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point()
p + theme_classic()
Run Code Online (Sandbox Code Playgroud) 我希望通过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) 我看了这个问题,但给定的方法似乎不适用于脚注(参见 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) 我想将多项式系数附加到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中的 levelplot(网格包)上显示数据值.如果有人帮我这样做,我会很感激.提前致谢.
我的问题与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)
如果子文档位于同一目录中,我可以使用以下代码.
<<child-demo, child=knitr-input-child.Rnw, eval=TRUE>>=
@
Run Code Online (Sandbox Code Playgroud)
我想知道如果它不在主文档的同一目录中,如何使用子文档.在此先感谢您的帮助和时间.