我有一个长的矢量字符串(DNA序列),最多可以添加到我的knitr报告输出中的几千个连续字符.RStudio在控制台中完美地处理文本包装,但是当我生成knitr html输出时,我只能看到一行文本,它只是在页面上运行.
RStudio输出

编织输出

任何调整knitr输出以包装文本的方法?
谢谢.
我想将一个命名向量或两个向量传递给dplyr :: recode.假设我有:
library(dplyr)
set.seed(1)
x <- c("customer", sample(c("a", "b", "c"), 10, replace = TRUE))
recode_tbl <- tibble(letter = letters[1:3], fruit = c("apple", "banana", "cranberry"))
Run Code Online (Sandbox Code Playgroud)
我想要做的是使用recode_tbl的列重新编码x,而不必手动指定对:
recode(x, a = "apple", b = "banana", c = "cranberry")
Run Code Online (Sandbox Code Playgroud)
就像是:
recode(x, as.name(recode_tbl$letter) = recode_tbl$fruit)
Run Code Online (Sandbox Code Playgroud)
这显然不起作用.我并不反对尝试NSE,但是如果有人能够得到足够的球,那就太棒了.
谢谢.
假设我有一个长字符串:pneumonoultramicroscopicsilicovolcanoconiosis。我想stringr::str_replace_all用其他字母替换某些字母。根据文档,str_replace_all可以采用命名向量并用值替换名称。这适用于 1 次替换,但对于多次替换,它似乎是迭代进行的,因此结果是对上一次迭代的替换。我不确定这是预期的行为。
library(tidyverse)
text_string = "developer"
text_string %>%
str_replace_all(c(e ="X")) #this works fine
[1] "dXvXlopXr"
text_string %>%
str_replace_all(c(e ="p", p = "e")) #not intended behaviour
[1] "develoeer"
Run Code Online (Sandbox Code Playgroud)
想要的结果:
[1] "dpvploepr"
Run Code Online (Sandbox Code Playgroud)
我通过引入一个新角色得到:
text_string %>%
str_replace_all(c(e ="X", p = "e", X = "p"))
Run Code Online (Sandbox Code Playgroud)
这是一个可用的解决方法,但很难推广。这是一个错误还是我的期望错误?
我还希望能够同时用n 个其他字母替换n个字母,最好使用两个向量(如“旧”和“新”)或命名向量作为输入。
reprex 已编辑以便于人类阅读
在生物学中,我们经常想要绘制剂量反应曲线.R包'drc'非常有用,基本图形可以轻松处理'drm models'.但是,我想将我的drm曲线添加到ggplot2中.
我的数据集:
library("drc")
library("reshape2")
library("ggplot2")
demo=structure(list(X = c(0, 1e-08, 3e-08, 1e-07, 3e-07, 1e-06, 3e-06,
1e-05, 3e-05, 1e-04, 3e-04), Y1 = c(0, 1, 12, 19, 28, 32, 35,
39, NA, 39, NA), Y2 = c(0, 0, 10, 18, 30, 35, 41, 43, NA, 43,
NA), Y3 = c(0, 4, 15, 22, 28, 35, 38, 44, NA, 44, NA)), .Names = c("X",
"Y1", "Y2", "Y3"), class = "data.frame", row.names = c(NA, -11L
))
Run Code Online (Sandbox Code Playgroud)
使用基本图形:
plot(drm(data = reshape2::melt(demo,id.vars = "X"),value~X,fct=LL.4(),na.action = na.omit),type="bars") …Run Code Online (Sandbox Code Playgroud) 我有一些分组数据,我想要位置躲避箱线图和位置躲避点,并且我想添加一条连接组内点的线。
library(tidyverse)
mock_data <- tibble::tribble(
~Subject, ~Marker, ~Cell_type, ~Value,
"1", "A", "B", 70L,
"2", "A", "B", 80L,
"3", "A", "B", 90L,
"1", "A", "T", 5L,
"2", "A", "T", 10L,
"3", "A", "T", 15L,
"1", "B", "B", 1L,
"2", "B", "B", 2L,
"3", "B", "B", 3L,
"1", "B", "T", 99L,
"2", "B", "T", 90L,
"3", "B", "T", 99L
)
Run Code Online (Sandbox Code Playgroud)
我已经尝试了 geom_line 中的各种分组,但似乎无法获得预期的输出
mock_data |>
ggplot(aes(x = Marker, y = Value)) +
geom_boxplot(aes(fill = Cell_type), alpha = 0.2) +
geom_point(aes(col = …Run Code Online (Sandbox Code Playgroud) 我不确定为什么 ggplot2 中的 mean_sdl 函数(来自 Hmisc)生成的误差线比手动生成的误差线宽得多并绘制均值 + sd 和均值 - sd。我的代码:
library(drc)
library(tidyverse)
test_dataset <-
structure(
list(
X = c(1e-10, 1e-08, 3e-08, 1e-07, 3e-07, 1e-06, 3e-06, 1e-05, 3e-05, 1e-04, 3e-04),
AY1 = c(0, 11, 125, 190, 258, 322, 354, 348, NA, 412, NA),
AY2 = c(3, 33, 141, 218, 289, 353, 359, 298, NA, 378, NA),
AY3 = c(2, 25, 160, 196, 345, 328, 369, 372, NA, 399, NA),
BY1 = c(3, NA, 11, 52, 80, 171, 289, 272, 359, …Run Code Online (Sandbox Code Playgroud) 我有一个要合并的 tibbles 列表(长度 = 5000)。他们都有相同的列,所以我想合并使用dplyr::bind_rows. 从表面上看,每个添加的小标题绑定行非常快,但是随着添加更多小标题,执行时间呈指数增长而不是线性增长。做了一些谷歌搜索后,它非常像这里观察到的错误:https : //github.com/tidyverse/dplyr/issues/1396。尽管应该在 bind_rows 内部修复了该错误,但我仍然看到每个 tibble 所用的时间呈指数增长。
library(foreach)
library(tidyverse)
set.seed(123456)
tibbles <- foreach(i = 1:200) %do% {
tibble(a = rnorm(10000),
b = rep(letters[1:25], 400),
c = rnorm(10000))
}
times <- foreach(i = 1:200) %do% {
system.time(tibbles[1:i] %>%
purrr::reduce(bind_rows))
}
times %>%
map_dbl(.f = ~.x[3]) %>%
plot(ylab = "time [s] per added tibble")
Run Code Online (Sandbox Code Playgroud)
任何想法为什么会出现这种情况以及如何解决它?
谢谢。
假设我有一个包含多个列的数据框,其中一些我想要转换。列名称定义需要使用什么转换。
library(tidyverse)
set.seed(42)
df <- data.frame(A = 1:100, B = runif(n = 100, 0, 1), log10 = runif(n = 100, 10, 100), log2 = runif(n = 100, 10, 100), log1p = runif(n = 100, 10, 100), sqrt = runif(n = 100, 10, 100))
trans <- list()
trans$log10 <- log10
trans$log2 <- log2
trans$log1p <- log1p
trans$sqrt <- sqrt
Run Code Online (Sandbox Code Playgroud)
理想情况下,我想使用一个across调用,其中列名称与反式函数名称相匹配,并且转换将即时执行。所需的输出如下:
df_trans <- df %>%
dplyr::mutate(log10 = trans$log10(log10),
log2 = trans$log2(log2),
log1p = trans$log1p(log1p),
sqrt = trans$sqrt(sqrt))
df_trans
Run Code Online (Sandbox Code Playgroud)
但是,我不想单独手动指定每个转换。在代表性示例中,我只有 4 …